gpt4 book ai didi

mvvm-light - 在Windows 8中使用MVVMLight通过ViewModel进行页面导航

转载 作者:行者123 更新时间:2023-12-03 08:24:19 25 4
gpt4 key购买 nike

我上周刚开始使用mvvm light开发全新的Windows 8应用程序。我对mvvmlight WP7导航很熟悉。我如何在Windows 8中实现相同的功能。有人可以提出一种更好的方法在Windows 8中实现相同的功能。我找到了一种解决方案,其中我们覆盖了vm中的onnavigated事件并处理了导航到其他页面的问题。但是我认为这种方法已经过时了。任何人都可以指导我正确实现。提前致谢。

最佳答案

我了解这不是您要找的确切答案,但这可能会为您提供一些思路。

就我而言,我不是在使用MVVMLight-而是我自己的简单MVVM实现。我将BindableBase类(默认VS 2012 RC模板随附)用于属性通知。我想,您可以使用MVVMLight为您提供一些基础架构,并可以通过以下内容进行补充。

为了进行导航,我定义了一个界面,如下所示:

public interface INavigationService
{
void Navigate(Type type);
void Navigate(Type type, object parameter);
void EnsureNavigated(Type pageType, object parameter);

bool CanGoBack { get; }
bool CanGoForward { get; }
void GoBack();
void GoForward();

IView CurrentView { get; }
}

并实现如下:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

public class NavigationService : INavigationService
{
private readonly Frame _frame;

public NavigationService(Frame frame)
{
_frame = frame;
_frame.Navigated += OnFrameNavigated;
}

private void OnFrameNavigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
var view = e.Content as IView;
if (view == null)
return;

var navMsg = new NavigationMessage()
{
Sender = this,
NewView = view,
Parameter = e.Parameter,
NavigationMode = (int)e.NavigationMode
};

EventManager.Current.Publish(navMsg);

//Anything that the parent needs to be notified should happen in of after this method
var viewModel = view.ViewModel;
if (viewModel != null)
viewModel.Initialise(e.Parameter);
}

public void Navigate(Type pageType)
{
DisposePreviousView();
_frame.Navigate(pageType);
}

public void Navigate(Type pageType, object parameter)
{
DisposePreviousView();
_frame.Navigate(pageType, parameter);
}

private void DisposePreviousView()
{
var currentView = this.CurrentView;
var currentViewDisposable = currentView as IDisposable;
if (currentViewDisposable != null)
{
currentViewDisposable.Dispose();
currentViewDisposable = null;
} //view model is disposed in the view implementation
}

public void EnsureNavigated(Type pageType, object parameter)
{
var currentView = this.CurrentView;
if (currentView == null || currentView.GetType() != pageType)
{
Navigate(pageType, parameter);
}
}

public IView CurrentView
{
get { return _frame.Content as IView; }
}


public bool CanGoBack
{
get { return _frame != null && _frame.CanGoBack; }
}

public void GoBack()
{
// Use the navigation frame to return to the previous page
if (_frame != null && _frame.CanGoBack) _frame.GoBack();
}

public bool CanGoForward
{
get { return _frame != null && _frame.CanGoForward; }
}

public void GoForward()
{
// Use the navigation frame to return to the previous page
if (_frame != null && _frame.CanGoForward) _frame.GoForward();
}

}

IView:
public interface IView : IDisposable
{
IViewModel ViewModel { get; }
void Refresh();
}

IViewModel:
public interface IViewModel : INotifyPropertyChanged, IDisposable
{
void Initialise(object parameter);
string ViewTitle { get; }
void Refresh();
}

最后,在XAML页面中,定义一个 Frame元素:
<Frame x:Name="ContentFrame" />

在页面的代码中:(这是我认为唯一丑陋的部分,但希望它不会太糟):
var _navigationService = new NavigationService(this.ContentFrame);

现在,您可以将 _navigationService传递给 View 模型。就我而言,我在页面的代码后面创建了viewmodel:
public HomePage()
{
this.InitializeComponent();

var _navigationService = NavigationService.GetFor(this.ContentFrame);

DataContext = new HomePageViewModel(_navigationService);

}

希望这可以帮助。

关于mvvm-light - 在Windows 8中使用MVVMLight通过ViewModel进行页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10971023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com