我不知道它是否是一个很好的实践,但我想将子目录用于目录“Views”和“ViewModels”
我现在有这个代码
namespace Project.Universal.ViewModels.Sales
{
public class SalesListViewModel:ViewModelBase
{
// Properties
private string _test;
private ICollection<Sale> _sales;
public string Test
{
get => _test;
set => SetProperty(ref _test, value);
}
public ICollection<Sale> Sales {
get => _sales;
private set
{
}
}
// Services
private readonly IStandardService<Sale> _salesService;
// Commands
public DelegateCommand Update { get;}
// Constructor
public SalesListViewModel(IStandardService<Sale> salesService)
{
Debug.WriteLine(">>>> Initializing SalesListViewModel, trying to set _salesService.");
_salesService = salesService;
Debug.WriteLine(">>>> Delegating command to update SalesListViewModel.");
Update = new DelegateCommand(async () =>
{
Sales = await _salesService.GetAsync();
Test = "Test";
});
}
}
}
这是我的观点页面
<mvvm:SessionStateAwarePage
x:Class="Project.Universal.Views.Sales.SalesListPage"
mvvm:ViewModelLocator.AutoWireViewModel="True"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Project.Universal.Views.Sales"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:mvvm="using:Prism.Windows.Mvvm"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="{StaticResource MediumLeftTopRightBottomMargin}">
<TextBlock>this is the Sales page</TextBlock>
<TextBlock Text="{Binding Test}"></TextBlock>
</Grid>
</mvvm:SessionStateAwarePage>
但是Prism没有正确地注入视图模型。取消这一点,Prism框架正在注入ShellViewModel,就像错误贝拉说:
错误:BindingExpression路径错误:'Project.Universal.ViewModels.ShellViewModel'上找不到'Test'属性。 BindingExpression:Path ='Test'DataItem ='Project.Universal.ViewModels.ShellViewModel'; target元素是'Windows.UI.Xaml.Controls.TextBlock'(Name ='null'); target属性是'Text'(类型'String')
对于组织,我想将此模式用于文件夹:
Views/Context/ViewPage.xaml for views
ViewModels/Context/ViewModel.cs for viewModels
我正在使用带有Prism 6.3框架的Windows Template Studio模板。 抱歉我的英文。
答案 0 :(得分:1)
几小时后我就解决了我的问题:
Windows Template Studio已更改ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver()
方法以保持与其他框架的兼容性。我刚刚改变了这个方法,就像我想要的那样有效。对于会遇到同样问题的其他人,这是代码。
班级App.xaml.cs
protected override async Task OnInitializeAsync(IActivatedEventArgs args)
{
await ThemeSelectorService.InitializeAsync().ConfigureAwait(false);
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
{
var viewName = viewType.FullName; // get full type name
var viewModelName = viewName
.Substring(0, viewName.Length - 4)
.Replace(".Views.", ".ViewModels.");
var viewModelAssembly = viewType.GetTypeInfo().Assembly.FullName;
//var viewModel
var viewModelTypeName = string.Format(CultureInfo.InvariantCulture, "{0}ViewModel, Project.Universal", viewModelName);
return Type.GetType(viewModelTypeName);
});
await base.OnInitializeAsync(args);
}