首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xamarin中将页绑定上下文设置为不同的视图模型时,如何将控件绑定到视图模型

在Xamarin中将页绑定上下文设置为不同的视图模型时,如何将控件绑定到视图模型
EN

Stack Overflow用户
提问于 2021-01-04 08:31:52
回答 1查看 549关注 0票数 1

我的内容页面绑定到视图模型,但现在我需要将一些组件绑定到另一个视图模型。我的问题是,因为我已经将页面的bindingcontext绑定到视图模型,所以当我尝试将元素绑定到第二个视图模型时,{ BindingContext }语句将在绑定到绑定上下文的视图模型中查找属性。

所以我的问题是:当bindingcontext被设置为不同的视图模型时,如何简单地将一个元素绑定到另一个视图模型,然后绑定到另一个视图模型?

我试过的那些没用的东西

  1. 定义了第二个视图模型。并尝试通过静态资源

进行绑定。

代码

代码语言:javascript
复制
    <ContentPage.Resources>
        <ResourceDictionary>
            <selectedDeal:DealsMViewModel x:Name="SelectedDeal" x:Key="SelectedDeal"/>
        </ResourceDictionary>
    </ContentPage.Resources>

代码

代码语言:javascript
复制
 <Image
     x:Name="CompanyImage"
     HeightRequest="200"
     Aspect="AspectFill"
     Source="{Binding Source={StaticResource SelectedDeal}, Path=DealImage}" />

我已经将图像元素放入堆栈布局中,设置了的BindingContext,并尝试了这样的绑定:

代码

代码语言:javascript
复制
 <Image
         x:Name="CompanyImage"
         HeightRequest="200"
         Aspect="AspectFill"
         Source="{Binding DealImage}"/>

有两种方法使绑定工作

  1. 将第二个视图模型定义为绑定到BindingContext的第一个视图模型的属性,然后实现如下的

图像

代码

代码语言:javascript
复制
<Image
x:Name="CompanyImage"
HeightRequest="200"
Aspect="AspectFill"
Source="{Binding secondViewModel.DealImage}"/>

设置图像源programatically.后的代码中的

代码

代码语言:javascript
复制
CompanyImage.Source = selectedDeal.DealImage;

,但是我真正想要实现的是,如果可能的话,在XAML部分中学习这种方法!任何帮助都非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-01-04 16:13:50

有几种方法可以实现你想要的.我会解释我处理ViewModel绑定的方式..。

  1. 创建新类,使用DependencyService容器管理器保存对每个视图模型的引用。我把这叫做ViewModelLocator。例如:

代码语言:javascript
复制
public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            DependencyService.Register<ILogService, LogService>();
            if (!DesignMode.IsDesignModeEnabled)
            {
                DependencyService.Register<IDataStore, MockDataStore>();
            }
            else
            {
                DependencyService.Register<IDataStore, DesignDataStore>();
            }
            DependencyService.Register<LoginViewModel>();
            DependencyService.Register<AboutViewModel>();
            DependencyService.Register<SettingsViewModel>();
        }
        public LoginViewModel Login => DependencyService.Get<LoginViewModel>();
        public AboutViewModel About => DependencyService.Get<AboutViewModel>();
        public SettingsViewModel Settings => DependencyService.Get<SettingsViewModel>();
    }

  1. 引用App.xaml中的该类

  1. --这将允许您通过引用定位器来绑定到任何视图模型。在下面的示例中,您的“主”ViewModel是LoginViewModel,但是下面的按钮绑定到关于ViewModel

代码语言:javascript
复制
<?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="Test.Views.LoginPage"
             BindingContext="{Binding Login, Source={StaticResource Locator}}"
             Title="LoginPage">
    <ScrollView>
        <Grid Margin="10,20,10,10" RowDefinitions="Auto,Auto,Auto,Auto">
            <Label Grid.Row="0" Text="Username:" TextColor="Black" FontSize="14"/>
            <Entry Grid.Row="1" x:Name="usernameEditor" Text="{Binding Username}" ClearButtonVisibility="WhileEditing" />
            <Label Grid.Row="2" Text="Password:" TextColor="Black" FontSize="14"/>
            <Entry Grid.Row="3" x:Name="passwordEditor" Text="{Binding Password}" IsPassword="True" ClearButtonVisibility="WhileEditing" />
            <Button Grid.Row="6" Text="Login" Command="{Binding About.LoginCommand, Source={StaticResource Locator}}"/>
        </Grid>
    </ScrollView>
</ContentPage>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65559689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档