In my UWP version app I have three file types that a user could click on and import into the app. I haven't been supporting OnFileActivated since users can import these file even in batch from the utilities menu. Now I want to launch the file with the app when clicked on. What I want to know is how do I do this using the debugger? Seems to me this could be done by supplying a command line argument though maybe not. Also there is little information from that point as how the MainPage receives the argument for the file clicked on. If I can debug I can figure that part out.
I have it working. This helped:
https://stackoverflow.com/questions/53102479/opening-the-start-page-after-opening-xamarin-uwp-app-via-file-type-associations
as well as this 2014 article I mentioned above and sample with is out-of-date and going away.
https://docs.microsoft.com/en-us/previous-versions/windows/apps/hh779669(v=win.10)
Here are snippets with the solution:
protected override void OnFileActivated(FileActivatedEventArgs e) { base.OnFileActivated(e); var rootFrame = new Frame(); Xamarin.Forms.Forms.Init(e); rootFrame.Navigate(typeof(MainPage), e); var p = rootFrame.Content as MainPage; p.FileEvent = e; p.AssocFile(); Window.Current.Content = rootFrame; Window.Current.Activate(); }
There are a few more actions that should be added that can be taken from the OnLaunch code in the UWP App.xaml.cs but this works.
And here is the snippet for the MainPage AssocFile():
private FileActivatedEventArgs _fileEventArgs = null; public FileActivatedEventArgs FileEvent { get { return _fileEventArgs; } set { _fileEventArgs = value; } } public void AssocFile() { this.InitializeComponent(); var path = FileEvent.Files[0].Path; LoadApplication(new Assoc.App(path)); }
From there I can pass the path to my my app's App.xaml.cs to act on the file.
Answers
If you want to debug the
OnFileActivated
try to run your project and keep your application active. When you click the file to open the application this method will be called. It will open the same instance which is run by the VS before.And if you want to send some information to your forms page you can consume MessagingCenter:
Thanks. That's one way to see if it worked or not but we really need to be able to debug maybe with a command line. Because my users can import files from my other programs those files can show up with my app icon and now they click on them and nothing happens. I want that to work since the code is there once the app knows what is happening to open the app with the file they've selected. I may try faking a file load by changing the startup from in the app startup. Implementing the OnFileActivated does bring up an error message now.
What do you mean about
we really need to be able to debug maybe with a command line
.When debugging a project we should keep it alive and then we can test the functions. Firstly, set up your developing app by Visual Studio. Then click the files to run into the
OnFileActivated
for debugging. And you can also pass objects to main page through messaging center.A command line switch with might emulate loading a file using OnFileActivate. You could specify the file then check when debugging. There is this old article and sample code for using OnFileActivate for Windows 8.1 and Windows Phone.
https://docs.microsoft.com/en-us/previous-versions/windows/apps/hh779669(v=win.10)
There is none for UWP at the moment but the gist of the sample code should work. Your message passing idea doesn't work BTW. I'll post an example on GitHub if I get it to working.
If you wanted to debug this approach does work or not try the normal ways to make it out. If you could use command line to open the file with your application type why not keep your application alive with debugging mode?
The link you posted tells how to handle the
OnFileActivated
but if you want to debug this behavior you must run your project with vs. Then the breakpoint can be triggered.I think messaging center should work if
OnFileActivated
fires. If not please check whether theOnFileActivated
has been called or not.I have it working. This helped:
https://stackoverflow.com/questions/53102479/opening-the-start-page-after-opening-xamarin-uwp-app-via-file-type-associations
as well as this 2014 article I mentioned above and sample with is out-of-date and going away.
https://docs.microsoft.com/en-us/previous-versions/windows/apps/hh779669(v=win.10)
Here are snippets with the solution:
There are a few more actions that should be added that can be taken from the OnLaunch code in the UWP App.xaml.cs but this works.
And here is the snippet for the MainPage AssocFile():
From there I can pass the path to my my app's App.xaml.cs to act on the file.