XAMLユーザーコントロール内では、Frameオブジェクトはnullです。
this.Frame.Navigate(typeof(FaxPropertiesPage));
Windows 8 XAMLユーザーコントロールを使用してページ間を移動するにはどうすればよいですか?XAMLページのCallistoFlyout内にコントロールを配置しました。
下の検索ボタンは、ユーザーを別のXAMLページに移動する必要があります。
XAMLユーザーコントロール内では、Frameオブジェクトはnullです。
this.Frame.Navigate(typeof(FaxPropertiesPage));
Windows 8 XAMLユーザーコントロールを使用してページ間を移動するにはどうすればよいですか?XAMLページのCallistoFlyout内にコントロールを配置しました。
下の検索ボタンは、ユーザーを別のXAMLページに移動する必要があります。
app.xaml.csのコードを正常に使用しました
Frame frame = Window.Current.Content as Frame;
次に、標準のナビゲートコードを使用しました。
良い方法とそうでない方法があります:
どちらもナビゲーションサービスから始まります。
public interface INavigationService
{
bool CanGoBack { get; }
void GoBack();
void GoForward();
bool Navigate<T>(object parameter = null);
bool Navigate(Type source, object parameter = null);
void ClearHistory();
event EventHandler<NavigatingCancelEventArgs> Navigating;
}
public class NavigationService : INavigationService
{
private readonly Frame _frame;
public NavigationService(Frame frame)
{
_frame = frame;
frame.Navigating += FrameNavigating;
}
#region INavigationService Members
public void GoBack()
{
_frame.GoBack();
}
public void GoForward()
{
_frame.GoForward();
}
public bool Navigate<T>(object parameter = null)
{
Type type = typeof (T);
return Navigate(type, parameter);
}
では、フレームはどこで入手できますか?App.xaml.csで
protected async 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;
}
// Create a Frame to act as the navigation context and navigate to the first page
var rootFrame = new Frame();
if (DesignMode.DesignModeEnabled)
SimpleIoc.Default.Register<INavigationService, DesignTimeNavigationService>();
else
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService(rootFrame));
ここではMVVMLightを使用しています。これにより、すべてのビューモデルが依存性注入を使用して作成され、それらのサービスが注入されるため、作業が楽になります。
MVVM Lightのようなものを使用しておらず、コードビハインドに依存している場合でも、これを機能させることができます。ナビゲーションサービスを静的にするだけです。
public class NavigationService : INavigationService
{
public static INavigationService Current{
get;set;}
blah blah blah
}
そして、App.xaml.csを次のように変更します。
protected async 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;
}
// Create a Frame to act as the navigation context and navigate to the first page
var rootFrame = new Frame();
NavigationService.Current= new NavigationService(rootFrame));
}
そして、次のように言うことで、アプリのどこからでもメインフレームにアクセスできます。
NavigationService.Current.Navigate<MyView>();
単純なコード(100%効率的ではない場合があります)は次のとおりです。
フレームframe=new Frame(); frame.Navigate(typeof(ExerciseAddPage)