AutoWirePartialView with prism does not work or badly used?

半世苍凉 提交于 2019-12-12 13:59:33

问题


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.

There is my Page :

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

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

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

There is my 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>

And this is my 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);
        }
    }
}

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

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

containerRegistry.RegisterForNavigation<ForecastDemoPage, ForecastDemoPageViewModel>();

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

Thanks !


回答1:


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

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

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

So what I needed is to register manually the ViewModel with the PartialView using ViewModelLocationProvider

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

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)



来源:https://stackoverflow.com/questions/53896129/autowirepartialview-with-prism-does-not-work-or-badly-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!