我正在尝试使用棱镜7.1 AutoWirePartialView
将PartialView
绑定到其viewModel。但是,绑定不起作用,或者至少将viewModel设置为PartialView
似乎不起作用,它仍然具有页面的BindingContext作为BindingContext。
有我的页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project.Core.Views.NotConnectedViews.ForecastDemoPage"
xmlns:carouselForecast="clr-namespace:Project.Core.Views.MainViews"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
x:Name="ForecastDemo"
BackgroundColor="{StaticResource PrimaryColorOne}" ControlTemplate="{StaticResource MainAppTemplate}">
<ContentPage.ToolbarItems>
<ToolbarItem Name="SearchForecast" Command="{Binding ShowSearchForecastDemoCommand}" Order="Primary" Icon="ic_search_white_24dp.png" Priority="0" />
</ContentPage.ToolbarItems>
<ContentView x:Name="ContentViewForecast" ControlTemplate="{StaticResource ForecastTownControlTemplate}">
<carouselForecast:ForecastPartialViewCarousel prism:ViewModelLocator.AutowirePartialView="{x:Reference ForecastDemo}"></carouselForecast:ForecastPartialViewCarousel>
</ContentView>
</ContentPage>
绑定:找不到“ DayWeatherForecasts”属性 'Project.Core.ViewModels.ForecastDemoPageViewModel',目标属性: 'Project.Core.Views.MainViews.ForecastPartialViewCarousel.ItemsSource'
如您所见,我将部分视图用作使用ContentPresenter
的{{1}}的{{1}}。
有我的PartialView:
ContentView
这是我的PartialView ViewModel :
ControlTemplate
当然<carousel:CarouselViewControl x:Name="carouselView"
Position="{Binding CarouselPosition}"
PositionSelectedCommand="{Binding PositionChanged}"
Orientation="Horizontal" AnimateTransition="True" IsSwipeEnabled="False"
ItemsSource="{Binding DayWeatherForecasts}" InterPageSpacing="10"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:carousel="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
x:Class="Project.Core.Views.MainViews.ForecastPartialViewCarousel">
<!-- Item template is defined here, removed for readability -->
</carousel:CarouselViewControl>
设置了一些存根值。为了简化可读性,我简化了viewModel。
我没有使用棱镜AutoWiring viewModel,因此在app.xaml.cs中:
namespace Project.Core.ViewModels
{
public class ForecastPartialViewCarouselViewModel : ViewModelBase
{
public ForecastPartialViewCarouselViewModel(IForecastService forecastService,
INavigationService navigationService) : base(navigationService)
{
InitStubForecasts();
}
private ObservableCollection<DayWeatherForecast> _dayWeatherForecasts;
public ObservableCollection<DayWeatherForecast> DayWeatherForecasts
{
get => _dayWeatherForecasts;
set => SetProperty(ref _dayWeatherForecasts, value);
}
}
}
问题:可能是我的DayWeatherForecasts
在ViewModels文件夹中,而我想绑定到该ViewModel的Partialview是在子文件夹containerRegistry.RegisterForNavigation<ForecastDemoPage, ForecastDemoPageViewModel>();
下吗?我应该创建一个PartialViewModel
文件夹并将其viewModel放在那里吗?
编辑:我尝试了此解决方案,但正如我期望的那样,它什么也不做。
如果没有,那么我不知道为什么它不起作用...
谢谢!
答案 0 :(得分:0)
好吧,所以我终于发现它不足以将其放入我的PartialView
prism:ViewModelLocator.AutowirePartialView="{x:Reference ForecastDemo}
当我在子文件夹中组织视图时,prism无法单独注册我的ViewModel
和我的PartialView
。
所以我需要使用ViewModel
向PartialView
手动注册ViewModelLocationProvider
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
ViewModelLocationProvider.Register<ForecastPartialViewCarousel,
ForecastPartialViewCarouselViewModel>();
}
这不仅与名称有关,而且与名称空间有关。如果我希望PartialView
设置正确的ViewModel
而不进行手动注册,则应该将PartialView
放在 Views 根文件夹中, ViewModels 根文件夹中对应的ViewModel
(具有命名约定)