带棱镜的AutoWirePartialView不起作用或使用不当? [英] AutoWirePartialView with prism does not work or badly used?

查看:83
本文介绍了带棱镜的AutoWirePartialView不起作用或使用不当?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用棱镜7.1 AutoWirePartialViewPartialView绑定到其viewModel.但是,绑定无效,或者至少将viewModel设置为PartialView似乎无效,它仍然具有页面的BindingContext作为BindingContext.

I'm trying to use prism 7.1 AutoWirePartialView to bind a PartialView to its viewModel. However, binding is not working, or at least, setting the viewModel to the PartialView does not seem to work, it still has the page's BindingContext as 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'

Binding: 'DayWeatherForecasts' property not found on 'Project.Core.ViewModels.ForecastDemoPageViewModel', target property: 'Project.Core.Views.MainViews.ForecastPartialViewCarousel.ItemsSource'

如您所见,我正在将部分视图用作使用ControlTemplateContentViewContentPresenter.

As you can see, I'm using the partial view as a ContentPresenter for a ContentView that uses a ControlTemplate.

有我的PartialView:

<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>

这是我的PartialView ViewModel :

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设置有一些存根值.为了简化可读性,我简化了viewModel.

Of course DayWeatherForecasts is set with some stub values. I simplified the viewModel for readability purpose.

我没有使用棱镜自动装配viewModel,因此在app.xaml.cs中:

I'm not using prism AutoWiring viewModel, so in app.xaml.cs :

containerRegistry.RegisterForNavigation<ForecastDemoPage, ForecastDemoPageViewModel>();

问题:可能是我的PartialViewModel在ViewModels文件夹中,而我想绑定到该ViewModel的Partialview是在子文件夹MainViews下吗?我应该创建一个MainViewsViewModel文件夹并将其viewModel放在那里吗?

Question : Could it be that my PartialViewModel is in the ViewModels folder and that the Partialview I want to be be bound to this ViewModel is under a subfolder MainViews ? Should I create a MainViewsViewModel folder and put my viewModel there ?

我尝试了此解决方案,但正如我期望的那样,它什么也不做.

EDIT : I tried this solution, but as I expected it does nothing.

如果没有,那么我不知道为什么它不起作用...

If not, then I don't know why it doesnt work ...

谢谢!

推荐答案

好,所以我终于发现它的不够不能将其放入我的PartialView

Ok so I finally found out that its not enough to put this to my PartialView

prism:ViewModelLocator.AutowirePartialView="{x:Reference ForecastDemo}

在子文件夹中组织视图时,prism无法单独注册我的ViewModelPartialView.

As I organized my views in subfolders, prism cannot register alone my ViewModel and my PartialView.

所以我需要使用ViewModelLocationProvider

protected override void RegisterTypes(IContainerRegistry containerRegistry) 
{ 
   ViewModelLocationProvider.Register<ForecastPartialViewCarousel,
   ForecastPartialViewCarouselViewModel>(); 
}

这不仅与名称有关,而且与名称空间有关.如果我希望PartialView设置正确的ViewModel而不进行手动注册,则应该将PartialView放在 Views 根文件夹中,并将相应的ViewModel放在 ViewModels 根文件夹(具有命名约定)

It's not only a matter of name, but of namespace too. If I wanted the PartialView to have the correct ViewModel set w/o registering it manually, I should have put my PartialView in the Views root folder and the corresponding ViewModel in the ViewModels root folder (with naming convention)

这篇关于带棱镜的AutoWirePartialView不起作用或使用不当?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆