I'm willing to create a metro style app similar to Windows Photo Viewer.
For that I need to add my app in the Openwith context menu. Also, the image file selected in the windows explorer should open in my Photo Viewer. How to open the app with the selected image file as a parameter?
Thanks in advance
Thanks a lot #Filip
I wrote the following code in OnFileActivated(FileActivatedEventArgs args) in App.xaml.cs
protected override void OnFileActivated(FileActivatedEventArgs args)
{
// TODO: Handle file activation
// The number of files received is args.Files.Size
// The first file is args.Files[0].Name
base.OnFileActivated(args);
StorageFile file = args.Files[0] as StorageFile;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
//rootFrame.SourcePageType = typeof(ImageViewer);
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (!rootFrame.Navigate(typeof(ImageViewer), file))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}
But, my app doesn't display anything on opening. So what according to you can be the flaw?
If you edit your Package.appxmanifest to add a File Type Association in the Declarations tab of the manifest editor - your app will become one of the apps that can open the file type that you specify. You then just need to add an override to your App class to handle the file activation as described in this article:
protected override void OnFileActivated(FileActivatedEventArgs args)
{
// TODO: Handle file activation
// The number of files received is args.Files.Size
// The first file is args.Files[0].Name
}
Related
In Windows 10, when I hit e.g. SHIFT + WIN + S I can take a screenshot of my screen.
In other cases I have my webcam that can take a picture, and so on.
All those scenarios have the feature to "Share" the captured image to another app, e.g. Mail, OneNote, etc.
I would like to register my own UWP app to be the recipient of such Share, so that the user can manipulate the captured image in my UWP app.
Is there a way to configure my UWP app to do this?
Is there a way to configure my UWP app to do this?
Yes, you could make your UWP app a receiver when you want to share some content from other apps.
Declare your app as a share target. Open the manifest file. Find the Declarations tab, then choose Share Target from the Available Declarations list, and then select Add.
Set the file types and formats based on your requirements in the Declarations. For example, if you need to receive a screenshot, you will need to add Bitmap in the Data format.
Handle share activation in the App.Xaml.cs by handling the Application.OnShareTargetActivated event.
I've made a simple test about this and you could refer to it. For more information about this, you could also check this document: Receive data
Manifest file:
App.xaml.cs:
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
ShareOperation shareOperation = args.ShareOperation;
if (shareOperation.Data.Contains(StandardDataFormats.Bitmap))
{
var imageStream = await shareOperation.Data.GetBitmapAsync();
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(ShareImagePage), imageStream);
Window.Current.Activate();
}
}
ShareImagePage:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Content != null)
{
IRandomAccessStreamReference imageReceived = null;
imageReceived = e.Parameter as IRandomAccessStreamReference;
using (var imageStream = await imageReceived.OpenReadAsync())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imgImage.Source = bitmapImage;
}
}
}
I am creating a uwp app with some media and video features, I have tested the app repeatedly on debug mode and it works just perfect as desired, after commenting and uncommenting of some code I was able to figure out that recursion of a specific method is causing the app to crash.
I also tried to display the exception with message dialog ( for release mode ). But no exception caught and app just freezes and crashed.
I run the app once on debug mode and then stop visual studio and then just directly open the app from my PC to test it on release mode.
MyScenario:
I am using OnFileActivated event to open video files directly on double click in my PC with my app. That works just fine. Below I will show you the code and reason for crashing. Please let me know how to solve this.
Thanks in advance.
Code
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//Some Media Stuff which works fine everytime even in release mode.
await speechRecognizer.CompileConstraintsAsync();// I am using speech input to play pause my media player.
ListenandAct();
}
private async void ListenandAct()
{
var result = await speechRecognizer.RecognizeAsync();
if (result.Text == "Pause" || result.Text == "pause")
{
Media.Pause();
}
else if (result.Text == "Play" || result.Text == "play")
{
Media.Play();
}
//ListenandAct(); //if I comment this line for recursion the app doesnt crash even in release mode, but If I uncomment it than app works perfect in debug mode but crashes in release mode.
}
OnFileActivated method in app.xaml.cs
protected async override void OnFileActivated(FileActivatedEventArgs args)
{
string ParentName = "";
if (args.Files.Count > 0)
{
Type TargetType = typeof(MainPage);
if (args.Files.Count == 1)
{
SingleFileToPlay = args.Files[0] as StorageFile;
TargetType = typeof(SingleFilePlay);
}
else if (args.Files.Count > 1)
{
ParentName = (await (args.Files[0] as StorageFile).GetParentAsync()).DisplayName;
MultiFilesToPlay = new List<StorageFile>();
TargetType = typeof(MultiFilePlay);
foreach (var file in args.Files)
{
MultiFilesToPlay.Add(file as StorageFile);
}
}
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
//if (rootFrame.Content == null)
//{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(TargetType, ParentName);
//}
// Ensure the current window is active
Window.Current.Activate();
}
}
NOTE
When I run a video file by double clicking it , it works perfect without crashing, but when my app is already running a video file and I double click another video file again then my app tries to run the second video file, the second video file is shown on the app as it is suposed to and then app freezes and crashes after a couple of seconds.
this is my first time to use stackoverflow :) ,
I was working with grid template app in windows 8 store app and i wanted to add welcome page after the splash screen loaded , to do that i tried to add this statement
this.Frame.Navigate(typeof(WelcomePage));
to the OnLaunched method that was created by default with the grid template in the App.xmal.cs. after i created the Welcomepage it self and i created a button in the welcome page to navigate to the GroupedItemsPage.
My logic isn't working.
this is the default method code in the App.xmal.cs if that will help
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
//Associate the frame with a SuspensionManager key
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Restore the saved session state only when appropriate
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (!rootFrame.Navigate(typeof(GroupedItemsPage), "AllGroups"))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}
I made a C# store app in Visual studio 2013. All of the sudden the startup form is changed, so its no longer the Mainpage. I cannot find how to resolve this problem.
I found a link where I could do something in the properties of my project, but I was unable to resolve the problem.
Does anyone know how to change the startup form for a C# store app in Visual Studio 2013?
edit: here is the OnLaunched method as told in comments.
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
Frame rootFrame = Window.Current.Content as Frame;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
await CatalogApp.Common.SuspensionManager.RestoreAsync();
}
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
CatalogApp.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame");
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
}
// Place the frame in the current Window+
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
There is probably is a navigation statement like: rootFrame.Navigate(typeof(ItemsPage), e.Arguments); in the
protected override async void OnLaunched(LaunchActivatedEventArgs e)
override in App.xaml.cs
If you are using this default, check typeof(ItemsPage) and the e.Argument statements.
Keep in mind, by default at activation,
the application redirects to the last opened window.
UPDATE
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
rootFrame.Navigate(typeof(MainPage), e.Arguments);
is a double navigation: first navigate to ShowItems, if succeeded navigate to MainPage. Why is that? I think this will lead to unexpected results.
Please try to remove
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
and check the results.
By the way: Which one is the page you want to start with?
I want to enter win32 desktop from a metro app when launch the metro app (press the app's tile on metro startup screen). One way is to open a file (e.g. a TXT file) when start the metro app. I add the following code logic into OnLaunched, sometimes it can open the file and enter desktop, but sometimes, it doesn't. Could someone help me? (just create a blank app in VS2012).
async protected override void OnLaunched(LaunchActivatedEventArgs args) {
// Do not repeat app initialization when already running, just ensure that the window //is active
if (args.PreviousExecutionState == ApplicationExecutionState.Running)
{
Window.Current.Activate();
return;
}
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Create a Frame to act navigation context and navigate to the first page
// var rootFrame = new Frame();
if (!rootFrame.Navigate(typeof(MainPage)))
{
throw new Exception("Failed to create initial page");
}
// Place the frame in the current Window and ensure that it is active
Window.Current.Content = rootFrame;
Window.Current.Activate();
{
string txtfile = #"Assets\a.txt";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(txtfile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
}
else
{
}
}
}
}
BatRT allows you to execute a batch file from a metro app. This could be used to run any desktop application from a metro app. As soon as the metro app starts execute a batch file to run your desired desktop application.