Toast 在单击时不向页面发送参数并且应用程序已经打开 C#
Toast Not sending parameter to page when clicked and app is already open C#
我有一个 Windows Phone 8.1 通用应用程序可以接收推送通知。单击 toast 时,它会打开应用程序并将参数发送到特定页面。我的问题是,只有当我点击吐司时应用程序关闭时它才有效。如果在我收到通知时该应用程序处于打开状态并单击它,它会将我定向到该应用程序,但不会将参数发送到特定页面 (ImageFullScreen)。你知道需要做什么才能做到这一点吗?
App.Xaml.Cs:
sealed partial class App : Application
{
public string NavigateText { get; set; }
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var launchString = e.Arguments;
(App.Current as App).NavigateText = launchString.ToString();
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
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();
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.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
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
MainPage.Xaml.Cs:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if ((App.Current as App).NavigateText == "")
{
await RefreshTodoItems();
}
else
{
items2 = await todoTable
.Where(todoItem => todoItem.Text == (App.Current as App).NavigateText)
.ToCollectionAsync();
TodoItem myItem = items2[0] as TodoItem;
Frame.Navigate(typeof(ImageFullScreen), myItem);
}
}
ImageFullScreen.Xaml.Cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var myObject = (TodoItem)e.Parameter;
img.Source = new BitmapImage(new Uri(myObject.ImageUri));
(App.Current as App).NavigateText = "";
}
问题是OnLaunched
方法中的if (rootFrame.Content == null)
没有执行,因为app已经打开了(frame的内容是页面)。只激活了window,没有调用MainPage
中的OnNavigatedTo
方法。
您可以尝试在 OnLaunched
方法中始终像这样启动导航(没有 if 语句):
rootFrame.Navigate(typeof(MainPage), e.Arguments);
// Ensure the current window is active
Window.Current.Activate();
我有一个 Windows Phone 8.1 通用应用程序可以接收推送通知。单击 toast 时,它会打开应用程序并将参数发送到特定页面。我的问题是,只有当我点击吐司时应用程序关闭时它才有效。如果在我收到通知时该应用程序处于打开状态并单击它,它会将我定向到该应用程序,但不会将参数发送到特定页面 (ImageFullScreen)。你知道需要做什么才能做到这一点吗?
App.Xaml.Cs:
sealed partial class App : Application
{
public string NavigateText { get; set; }
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var launchString = e.Arguments;
(App.Current as App).NavigateText = launchString.ToString();
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
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();
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.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
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
MainPage.Xaml.Cs:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if ((App.Current as App).NavigateText == "")
{
await RefreshTodoItems();
}
else
{
items2 = await todoTable
.Where(todoItem => todoItem.Text == (App.Current as App).NavigateText)
.ToCollectionAsync();
TodoItem myItem = items2[0] as TodoItem;
Frame.Navigate(typeof(ImageFullScreen), myItem);
}
}
ImageFullScreen.Xaml.Cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var myObject = (TodoItem)e.Parameter;
img.Source = new BitmapImage(new Uri(myObject.ImageUri));
(App.Current as App).NavigateText = "";
}
问题是OnLaunched
方法中的if (rootFrame.Content == null)
没有执行,因为app已经打开了(frame的内容是页面)。只激活了window,没有调用MainPage
中的OnNavigatedTo
方法。
您可以尝试在 OnLaunched
方法中始终像这样启动导航(没有 if 语句):
rootFrame.Navigate(typeof(MainPage), e.Arguments);
// Ensure the current window is active
Window.Current.Activate();