设置我的局部视图,不起作用xamarin [英] Setting my partial View, not working xamarin

查看:86
本文介绍了设置我的局部视图,不起作用xamarin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我开始使用xamarine的Prism框架,但是在实现跨视图的连接时遇到了一些问题.

我有此文件夹"Views",并且在其中有另一个名为PartialViews的文件夹.现在在PartialViews中,我有一个名为"Header.xaml"的contentPage. 现在,我想将此文件访问到位于Views文件夹中的Index.xaml视图.我也想将"Header.xaml"连接到其他视图,例如,我想将其连接到"Orders.xaml"视图.

我的Header.xaml文件如下:

        <?xml version="1.0" encoding="utf-8" ?>
        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:prism="http://prismlibrary.com"
                     xmlns:local="clr-namespace:PROJECTX.Views"
                     prism:ViewModelLocator.AutowireViewModel="True"
                     x:Class="PROJECTX.Views.Header">

            <StackLayout>
                <Label Text="Trying partial views" />
            </StackLayout>

        </ContentPage>

我的Index.xaml如下:

        <?xml version="1.0" encoding="utf-8" ?>
        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:combobox="clr-namespace:Syncfusion.XForms.ComboBox;assembly=Syncfusion.SfComboBox.XForms"
        xmlns:ListCollection="clr-namespace:System.Collections.Generic;assembly=mscorlib"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:prism="http://prismlibrary.com"
                     xmlns:local="clr-namespace:PROJECTX.Views"
                     prism:ViewModelLocator.AutowireViewModel="True"
                     x:Class="PROJECTX.Views.Index"
                     x:Name="selfi">


            <ScrollView>
                <local:Header mvvm:ViewModelLocator.AutowirePartialView="{x:Reference selfi}" />

                <combobox:SfComboBox x:Name="comboBox">
                    <combobox:SfComboBox.ComboBoxSource>
                        <ListCollection:List x:TypeArguments="x:String">
                            <x:String>Rendit sipas: Me te kerkuara</x:String>
                            <x:String>Rendit sipas: Te fundit</x:String>
                            <x:String>Rendit sipas: Alfabetit</x:String>
                        </ListCollection:List>
                    </combobox:SfComboBox.ComboBoxSource>
                </combobox:SfComboBox>


            </ScrollView>

        </ContentPage>

我还使用以下视图模型在App.xaml.cs上注册了PartialViews文件夹的路由:

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
          {
              containerRegistry.RegisterForNavigation<NavigationPage>();
              containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
              containerRegistry.RegisterForNavigation<Index, IndexViewModel>();
              //containerRegistry.RegisterForNavigation<Header, HeaderViewModel>();
              ViewModelLocationProvider.Register<Header, HeaderViewModel>();
          }

现在我遇到了一些错误...

1. The property 'Content' is set more than once. 
2. The attachable property 'AutowirePartialView' was not found in type 'ViewModelLocator'.
3. Property 'Content' does not support values of type 'Header'.

现在我知道这可能是一个菜鸟问题,但是我似乎无法使它用于处理部分视图问题. 我对xamarin中的部分视图的理解正确吗?我应该从视图中调用局部视图.对吗?

任何帮助我都会非常感激.

解决方案

因此,您必须了解,在Xamarin表单中,页面只能有一个ContentPage对象,因此,您会收到错误消息:内容设置超过一次".但是,您可以在单个页面中包含多个布局 这里简要介绍了页面与布局

由于您要使用棱镜"PartialViews",因此引用它们的一种常用方法是模板".因此,您将拥有一个共享的HeaderTemplate文件.但是,您可以使用ContentView对象(属于Layout类型)来代替ContentPage对象,并在其中定义标头的内容. (官方文档)

然后您可以使用XAML中的此行将其添加到任何页面,就像添加常规布局一样:

<templates: HeaderTemplate/>

Now I got started with Prism Framework for xamarine, but I'm having a little problem achieving connections across views.

I have this folder "Views" and inside it I have another folder called PartialViews. Now inside PartialViews I have a contentPage called "Header.xaml". Now I would like to atach this file to the Index.xaml view, which is located in the Views folder. I would like to atach the "Header.xaml" to other views also, like for example I would like to atach it to the "Orders.xaml" view.

My Header.xaml file is as follows :

        <?xml version="1.0" encoding="utf-8" ?>
        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:prism="http://prismlibrary.com"
                     xmlns:local="clr-namespace:PROJECTX.Views"
                     prism:ViewModelLocator.AutowireViewModel="True"
                     x:Class="PROJECTX.Views.Header">

            <StackLayout>
                <Label Text="Trying partial views" />
            </StackLayout>

        </ContentPage>

While my Index.xaml is as follows:

        <?xml version="1.0" encoding="utf-8" ?>
        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:combobox="clr-namespace:Syncfusion.XForms.ComboBox;assembly=Syncfusion.SfComboBox.XForms"
        xmlns:ListCollection="clr-namespace:System.Collections.Generic;assembly=mscorlib"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:prism="http://prismlibrary.com"
                     xmlns:local="clr-namespace:PROJECTX.Views"
                     prism:ViewModelLocator.AutowireViewModel="True"
                     x:Class="PROJECTX.Views.Index"
                     x:Name="selfi">


            <ScrollView>
                <local:Header mvvm:ViewModelLocator.AutowirePartialView="{x:Reference selfi}" />

                <combobox:SfComboBox x:Name="comboBox">
                    <combobox:SfComboBox.ComboBoxSource>
                        <ListCollection:List x:TypeArguments="x:String">
                            <x:String>Rendit sipas: Me te kerkuara</x:String>
                            <x:String>Rendit sipas: Te fundit</x:String>
                            <x:String>Rendit sipas: Alfabetit</x:String>
                        </ListCollection:List>
                    </combobox:SfComboBox.ComboBoxSource>
                </combobox:SfComboBox>


            </ScrollView>

        </ContentPage>

I also registred on my App.xaml.cs the routing of the PartialViews folder with the view model like this:

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
          {
              containerRegistry.RegisterForNavigation<NavigationPage>();
              containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
              containerRegistry.RegisterForNavigation<Index, IndexViewModel>();
              //containerRegistry.RegisterForNavigation<Header, HeaderViewModel>();
              ViewModelLocationProvider.Register<Header, HeaderViewModel>();
          }

Now I get a few errors...

1. The property 'Content' is set more than once. 
2. The attachable property 'AutowirePartialView' was not found in type 'ViewModelLocator'.
3. Property 'Content' does not support values of type 'Header'.

Now I know this might be a rookie question, but I just can't seem to get it to work this partial views thing. Is my understanding of partial views in xamarin correct? I'm supposed to call the partial view from the view.. correct?

Any help I would really really appreciate.

解决方案

So you have to understand that in Xamarin forms, you can only have one ContentPage object for a page, hence you are getting the error "Content is set more than once". You can however have multiple Layouts inside a single page Here’s a quick snippet explaining pages versus layouts

Since you’re looking to use the Prism "PartialViews", a common way of referencing them is a "Template". So you’d have a HeaderTemplate file as you have shared. But instead of a ContentPage object, you would use a ContentView object (which is of type Layout) and you would define the content of the header inside that. (Official Docs)

And then you can add it to any page using this line inside your XAML as you would add a regular layout:

<templates: HeaderTemplate/>

这篇关于设置我的局部视图,不起作用xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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