gpt4 book ai didi

c# - Mvvm Light、UWP 和代码隐藏

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:04 24 4
gpt4 key购买 nike

基于this excellent presentation来自 Xamarin Evolve 2014 的 Laurent Bugnion,我正在尝试创建我的第一个 UWP/MVVM Light 应用程序。

我创建了一个非常简单的 Article : ObservableObject 类,它具有 2 个字符串属性:RéférenceDésignation

在与文章 ListView 关联的 View 模型中,我有一个创建新文章的操作:

    public ArticlesViewModel(IArticleService dataService, INavigationService navigationService)
{
ArticleService = dataService;
NavigationService = navigationService;

CréeArticleCommand = new RelayCommand(CréeArticle);
}

public RelayCommand CréeArticleCommand { get; private set; }

private void CréeArticle()
{
if (!CréeArticleCommand.CanExecute(null))
return;

NavigationService.NavigateTo(ViewModelLocator.ArticleDetail_Key,
new ArticleViewModel(new Article(),
ArticleService,
NavigationService));
}

这是我的文章详细信息 View 的 XAML:

<!-- language: xaml -->
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalTest1.UWP.Articles"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Editors="using:DevExpress.UI.Xaml.Editors"
x:Class="UniversalTest1.UWP.Articles.Article_Detail"
mc:Ignorable="d"
xmlns:vm="clr-namespace:UniversalTest1.Data.ViewModels.Articles;assembly=UniversalTest1.Data"
d:DataContext="{d:DesignInstance Type=vm:ArticleViewModel, IsDesignTimeCreatable=True}">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="Référence :" HorizontalAlignment="Left" Margin="24,15,0,0" VerticalAlignment="Top"/>
<TextBlock Text="Désignation :" HorizontalAlignment="Left" Margin="10,52,0,0" VerticalAlignment="Top"/>

<Editors:TextEdit Text="{Binding Article.Référence, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,8,0,0" VerticalAlignment="Top" Width="300"/>
<Editors:TextEdit Text="{Binding Article.Désignation, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,45,0,0" VerticalAlignment="Top" Width="500"/>

<Button Content="Sauver" Command="{Binding SauverCommand}" HorizontalAlignment="Left" Margin="102,84,0,0" VerticalAlignment="Top"/>
</Grid>
</Page>

我的问题是我必须在我的页面后面的代码中定义 DataContext :

public sealed partial class Article_Detail : Page
{
public Article_Detail()
{
this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
DataContext = (ArticleViewModel)e.Parameter;
}
}

有没有办法保持 Xaml 页面的 d:DataContext 部分中定义的设计时 DataContext,并在运行时从 Navigation 参数获取 DataContext?

我的目标是尽可能减少后台代码中的代码量。所以我也想在 XAML 中定义运行时 DataContext。

最佳答案

您可以使用依赖注入(inject)为您的 View 模型创 build 计或运行时服务实例。使用 View 模型定位器,您可以执行如下操作:

public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

if (ViewModelBase.IsInDesignModeStatic)
{
if (!SimpleIoc.Default.IsRegistered<IArticleService>())
{
SimpleIoc.Default.Register<IArticleService, DesignArticleService>();
}
}
else
{
if (!SimpleIoc.Default.IsRegistered<IArticleService>())
{
SimpleIoc.Default.Register<IArticleService, ArticleService>();
}
}

SimpleIoc.Default.Register<ArticleViewModel>();
}

public ArticleViewModel ArticleViewModel => ServiceLocator.Current.GetInstance<ArticleViewModel>();
}

然后在您的 App.xaml 中注册定位器

<Application
x:Class="UniversalTest1.App" // your namespace
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="using:UniversalTest1.Data.ViewModels"> // your namespace

<Application.Resources>

<ResourceDictionary>
<viewModel:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>

</Application.Resources>

</Application>

然后您可以像这样在您的 xaml 中引用它:

<Page
...
DataContext="{Binding ArticleViewModel, Source={StaticResource Locator}}">

您也可以在这里查看示例代码 https://mvvmlight.codeplex.com/SourceControl/latest#Samples/Flowers/Flowers.Data/ViewModel/ViewModelLocator.cs

关于c# - Mvvm Light、UWP 和代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958738/

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