具有不同视图模型的同一视图上的 Xamarin 多重绑定上下文

若昂·莫雷拉

我有一个ContentPage有 2 个不同的 ViewModels ( RssFornecedores, RssProdutos) 并且我在绑定它们时遇到了问题,ContentPage我试图做这样的事情:

多个 BindingContext 在同一个 ContentPage 上,两个不同的视图?

但它没有工作,因为以下错误:

类型 'viewModels:RssProdutos' 用作标记扩展,但不是从 MarkupExtension 派生的

如果我添加为 aStaticResource BindingContext="{Binding Produto, Source = {StaticResource viewModels:RssProdutos}}"我得到错误:

未找到关键静态视图模型的静态资源:RssProdutos'

UnsolicitedNotificationEvent.html:

<?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:viewModels="clr-namespace:AppQ4evo.ViewModels" 
             xmlns:controls="clr-namespace:AppQ4evo.CSS"
             x:Class="AppQ4evo.Views.Documentos"           
             BackgroundColor="White">

    <ContentPage.BindingContext>
        <viewModels:RssTerceiros />
    </ContentPage.BindingContext>

    <!--Common Styles Starts-->
    <ContentPage.Resources>
        <ResourceDictionary>

            <Color x:Key="LightGreenColor">#2FA999</Color>
            <Color x:Key="BorderColor">#D8D8D8</Color>

            <Style x:Key="LableStyle" TargetType="Label">
                <Setter Property="TextColor" Value="#666666" />
                <Setter Property="FontSize" Value="Large" />
            </Style>
            <Style x:Key="FrameStyle" TargetType="Frame">
                <Setter Property="HasShadow" Value="False" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="CornerRadius" Value="5" />
                <Setter Property="BorderColor" Value="{StaticResource BorderColor}" />
            </Style>
            <Style x:Key="EntryStyle" TargetType="Entry">
                <Setter Property="HeightRequest" Value="40"></Setter>
                <Setter Property="Margin" Value="5,0,0,0"></Setter>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <!--Common Styles Ends-->

    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="iconAttach" Text="Documentos" Order="Primary" Icon="attach.png" Priority="-1" Clicked="Button_Clicked_AnexosAsync" />
    </ContentPage.ToolbarItems>

    <NavigationPage.TitleView>
        <StackLayout Orientation="Horizontal" VerticalOptions="Center" HorizontalOptions="Center">
            <Label Text="Documentos" FontSize="18" FontAttributes="Bold" TextColor="White" VerticalTextAlignment="Center" />
        </StackLayout>
    </NavigationPage.TitleView>

    <ContentPage.Content>
        <AbsoluteLayout Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <StackLayout>
                <Grid x:Name="docs" Padding="-2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <StackLayout Grid.Row="0" Margin="10,20">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="360" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Frame Style="{StaticResource FrameStyle}">
                                <controls:CustomEntry x:Name="DataDocEntry" Grid.Column="0" Text="{Binding Date, StringFormat='{0:MMMM dd, yyyy}', Source={x:Reference MainDatePicker}}" IsEnabled="false" Style="{StaticResource EntryStyle}" />
                            </Frame>
                            <DatePicker x:Name="MainDatePicker" Grid.Column="1" MinimumDate="1/1/2018" MaximumDate="12/31/2030" HeightRequest="30" BackgroundColor="Gray" Opacity="0"  />
                            <Image VerticalOptions="Center" Grid.Column="1" HorizontalOptions="End" Source="icalendar.png" HeightRequest="30" WidthRequest="30">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="Calendario_Clicked" NumberOfTapsRequired="1" />
                                </Image.GestureRecognizers>
                            </Image>
                        </Grid>
                        <Frame Style="{StaticResource FrameStyle}">
                            <controls:CustomEntry x:Name="NumExternoEntry" Placeholder="Número Externo" Keyboard="Numeric" />
                        </Frame>
                        <Frame Style="{StaticResource FrameStyle}">
                            <controls:CustomPicker x:Name="FornecedorEntry" Title="Selecionar Fornecedor"  />
                        </Frame>
                    </StackLayout>

                    <StackLayout Grid.Row="1" Margin="0,-10">
                        <Button x:Name="btnAdd" Text="Adicionar" Clicked="btnAddPopup_Clicked" />
                    </StackLayout>

                    <Grid x:Name="ListViewHeaders" Grid.Row="2" Margin="0,20,0,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="60" />
                            <ColumnDefinition Width="65" />
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="25" />
                            <ColumnDefinition Width="60" />
                        </Grid.ColumnDefinitions>
                        <BoxView Color="Black" Grid.Row="0" Grid.ColumnSpan="8" HeightRequest="0.5" />
                        <BoxView Color="DimGray" Grid.Row="0" Grid.ColumnSpan="8" />
                        <Label Grid.Column="1" Text="Produto" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                        <Label Grid.Column="2" Text="Descrição" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                        <Label Grid.Column="3" Text="Observação" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                        <Label Grid.Column="4" Text="Qtd" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                        <Label Grid.Column="5" Text="Valor Unitário" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                        <Label Grid.Column="6" Text="IVA" XAlign="Center" YAlign="Center" TextColor="White" FontAttributes="Bold" FontSize="Small"/>
                    </Grid>
                    <ListView x:Name="DocumentsList" ItemsSource="{Binding DocumentsList, Source = {viewModels:RssProdutos}}" Grid.Row="2" HasUnevenRows="True" Margin="0,60" BackgroundColor="White">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="30"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="30"></ColumnDefinition>
                                                <ColumnDefinition Width="50"></ColumnDefinition>
                                                <ColumnDefinition Width="70"></ColumnDefinition>
                                                <ColumnDefinition Width="80"></ColumnDefinition>
                                                <ColumnDefinition Width="30"></ColumnDefinition>
                                                <ColumnDefinition Width="50"></ColumnDefinition>
                                                <ColumnDefinition Width="45"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Label x:Name="ProdutoLabel" Grid.Row="0" Grid.Column="1" BindingContext="{Binding Produto, Source = {viewModels:RssProdutos}}" Text="{Binding Produto}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                                            <!--<Label Grid.Row="0" Grid.Column="2" Text="{Binding Descricao}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                                            <Label Grid.Row="0" Grid.Column="3" Text="{Binding Observacao}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                                            <Label Grid.Row="0" Grid.Column="4" Text="{Binding Quantidade}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                                            <Label Grid.Row="0" Grid.Column="5" Text="{Binding ValorUnitario}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>
                                            <Label Grid.Row="0" Grid.Column="6" Text="{Binding Iva}" XAlign="Center" YAlign="Center" TextColor="Black" FontAttributes="Bold"/>-->
                                        </Grid>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </StackLayout>
            <ContentView x:Name="popupAddDocumentView" Margin="40,120" Padding="10,0" IsVisible="false">
                <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                    <StackLayout Orientation="Vertical" HeightRequest="400" WidthRequest="300" BackgroundColor="Gray">
                        <Image HorizontalOptions="End" Source="close.png" HeightRequest="20" WidthRequest="20">
                            <Image.GestureRecognizers>
                                <TapGestureRecognizer Tapped="OnImageNameTapped_ClosePopup" NumberOfTapsRequired="1" />
                            </Image.GestureRecognizers>
                        </Image>
                        <Entry x:Name="ProdutoEntry" Margin="20,0,20,0" Placeholder="Produto" />
                        <Entry x:Name="DescricaoEntry" Margin="20,0,20,0" Placeholder="Descrição" />
                        <Entry x:Name="ObservacaoEntry" Margin="20,0,20,0" Placeholder="Observação" />
                        <Entry x:Name="QuantidadeEntry" Margin="20,0,20,0" Placeholder="Quantidade" />
                        <Entry x:Name="ValorUnitarioEntry" Margin="20,0,20,0" Placeholder="Valor" />
                        <Entry x:Name="IvaEntry" Margin="20,0,20,0" Placeholder="IVA" />
                        <Button x:Name="btnAddDocument" Margin="20,0,20,0" Clicked="btnAddDocument_Clicked" Text="Adicionar" />
                    </StackLayout>
                </StackLayout>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage> 

最好的问候

风趣
  • 如果你的代码中没有它的实例,你需要RssProdutos通过给它一个键来在你的 xaml 中创建一个作为资源的实例
<ContentPage.Resources>
     <viewModels:RssProdutos x:Key="RssProdutos "/>
</ContentPage.Resources>
...
<ListView x:Name="DocumentsList" ItemsSource="{Binding DocumentsList, Source={StaticResource RssProdutos}}"
          Grid.Row="2" HasUnevenRows="True" Margin="0,60" BackgroundColor="White">
  • 如果您RssProdutos的代码隐藏(属性)中已经有一个实例,则无需添加另一个作为静态资源的实例,您只需绑定到它即可:
RssProdutos RssProdutosVm {get; set;} = new RssProdutos();
<ContentPage x:Name="thispage" 
...
>
...
<ListView x:Name="DocumentsList" ItemsSource="{Binding DocumentsList, Source={Binding Source={x:Reference thispage}, Path=RssProdutosVm}}"
          Grid.Row="2" HasUnevenRows="True" Margin="0,60" BackgroundColor="White">

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

视图:额外的上下文

如何在Xamarin Forms中将ListView按钮的绑定上下文设置为父级的绑定上下文

在视图模型和视图之间使用MVVM进行WPF数据上下文绑定

Xamarin形式的数据模板视图单元内的绑定上下文

绑定上下文更改时的列表视图会清空按钮文本

xamarin mvvmcross 5 c#片段确保绑定上下文集对话框

Xamarin形式理解ObservableCollection绑定上下文

如何将列表视图项的上下文菜单绑定到ICommand

Xamarin.Forms:具有绑定上下文时单独访问布局

在视图页面Xamarin中绑定多个模型

Xamarin Forms将两个属性从单个控件绑定到不同的上下文

Xamarin表单中的视图到不同视图模型的绑定

Django在单个视图中具有多个上下文

WPF MVVM Light数据绑定上下文菜单以列出视图项

如何绑定到与样式值继承的数据上下文不同的数据上下文?

具有自己上下文绑定的类

在两个视图之间传输数据:上下文绑定

任何带有类型上下文绑定的

如何将TabControl内容的数据上下文绑定到ObservableCollection中的视图模型的实例

Xamarin 表单绑定上下文

将多个视图绑定到同一个数据上下文 (WPF-MVVM)

SAPUI5:视图的绑定上下文未解除绑定

xamarin android 中带有 RecyclerView 的上下文操作栏?

具有类属性的 Xamarin 绑定上下文

将图像控件绑定到数据网格数据上下文(视图模型)

具有上下文绑定的类型类隐式

创建从视图模型集合类到数据上下文中的属性的绑定

详细视图上下文

为什么视图绑定和上下文绑定无法检测上下文中存在的隐式