【问题标题】:Setting my partial View, not working xamarin设置我的部分视图,不工作 xamarin
【发布时间】:2020-04-26 23:05:13
【问题描述】:

现在我开始使用 Prism Framework for xamarine,但在实现跨视图连接时遇到了一点问题。

我有这个文件夹“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 中部分视图的理解是否正确?我应该从视图中调用部分视图..正确吗?

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms prism


    【解决方案1】:

    因此您必须了解,在 Xamarin 表单中,一个页面只能有一个 ContentPage 对象,因此您会收到错误“内容设置不止一次”。但是,您可以在单个页面中拥有多个布局 Here 是一个快速的 sn-p 解释页面与布局

    由于您希望使用 Prism“PartialViews”,因此引用它们的常用方法是“模板”。因此,您将拥有一个已共享的 HeaderTemplate 文件。但是,您将使用 ContentView 对象(属于 Layout 类型)而不是 ContentPage 对象,并且您将在其中定义标题的内容。 (Official Docs)

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

    &lt;templates: HeaderTemplate/&gt;

    【讨论】:

    • 不@saamer ..我想我的概念很清楚...我认为你搞错了伙计。让我从 Prism 文档中重新表述一下:“~Partial View 的概念是支持可以跨多个页面重用的自定义布局,并通过允许自定义布局依赖于它自己的 ViewModel 来消除 ViewModel 逻辑重复。~”这就是我想要完成的。我已经知道 xamarin 是什么,我在 2 年前开发了 2 个应用程序。现在我再次回到它,并注意到有一些变化......也许你应该看看 Prism 框架:)
    • 啊,我明白了。刚刚注意到棱镜标签。让我更新我的答案
    • 仍然@saamer 我得到同样的错误。我更新了 Header 视图,删除了 contentPage 并添加了 ContentView。但没有运气。也许你可以看看这里,github.com/PrismLibrary/Prism/releases/tag/v7.1.0 看看你是否做对了,因为我认为你比我更有经验。
    • @Pavel 不要忘记添加 XAML 编译以获得更好的错误报告。 [assembly: XamlCompilation (XamlCompilationOptions.Compile)] docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xamlc 此外,您不能只将 XAML 代码更改为 ContentView 并保留 Xaml.cs 页面以继承 ContentPage 类型。添加一个 ContentView 类型的新类,这样可以减少混淆。
    • 另外@Pavel,您似乎仍然错过了关于 ContentPage(页面)和 ContentViews(布局)的要点。您不能将 Index contentPage 放置在 MainPage 或任何其他页面中。您将索引页面保留为您将导航到的页面,并且它将包含您的 Header 模板。我强烈建议只是恢复到应用程序的工作状态,然后将模板添加到现有页面,以便您理解它
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2013-02-21
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多