簡體   English   中英

Xamarin Forms:如何正確地將來自兩個視圖模型的數據綁定到一個視圖?

[英]Xamarin Forms: How can i correctly bind data from two view models to a single view?

這是用於測試目的的短代碼。 問題是 UI 沒有顯示來自與 ViewModelB 綁定的 Label 的文本。 在調試時我在hover中將鼠標在xaml上從Label的文本上移開,我看到正確的綁定數據在那里,但UI根本不會顯示。 使用 ViewModelA 沒有問題。

在 XAML 中:

<StackLayout>
  <StackLayout>
        <StackLayout.BindingContext>
            <testbinding:ViewModelA/>
        </StackLayout.BindingContext>
        <Button Command ="{Binding Get}"/>
   </StackLayout>
    <StackLayout>
        <StackLayout.BindingContext>
            <testbinding:ViewModelB/>
        </StackLayout.BindingContext>
        <Label Text="{Binding Metadata}"/>
    </StackLayout>

ViewModelA:其中 BaseViewModel 是 INotifyPropertyChanged 接口

public ViewModelA:BaseViewModel
{   
 public ViewModelA()
 {
        Get = new Command(SendText);
        vmB = new ViewModelB();
 }
 ViewModelB vmB;
 public ICommand Get { get; }
 private void SendText()
 {
     string data = "someText";
     vmB.GetMetadata(data);
 }
}

ViewModelB 是這樣的:

class ViewModelB:BaseViewModel
{
    private string _metadata = string.Empty;
    public string Metadata
    {
        get { return _metadata; }
        set
        {
            _metadata = value;
            OnPropertyChanged();
        }
    }
    GetMetadata()
    {
    Metadata = "Some text";
    }
}

在 ViewModelA 中有更多我需要的屬性,而在 ViewModelB 中只是一個從 function 獲取數據的屬性。 我可以從它們中只制作一個 ViewModel,效果很好,但我試圖讓它們更小且更有條理。 我已經嘗試了很多場景並且變得非常令人沮喪。 感謝您的幫助。

在 xaml 文件的第二個 StackLayout 中,您沒有將其 BindingContext 屬性綁定到來自 ViewModelA 的 ViewModelB 實例,而是創建一個新的。

這是適合您的工作解決方案:

    public class ViewModelA : BaseViewModel
    {
        public ViewModelB ViewModelB { get; }
        public ICommand GetMetadataCommand { get; }

        public ViewModelA()
        {
            ViewModelB = new ViewModelB();
            GetMetadataCommand = new Command((_) => GetMetadata());
        }

        private void GetMetadata()
        {
            string data = "someText";
            ViewModelB.GetMetadata(data);
        }
    }

    public class ViewModelB : BaseViewModel
    {
        private string _metadata;
        public string Metadata
        {
            get { return _metadata; }
            set 
            {
                _metadata = value;
                OnPropertyChanged();
            }
        }

        public void GetMetadata(string data)
        {
            Metadata = data;
        }
    }

XAMl:

  <StackLayout>
        <StackLayout x:Name="StackLayout1">
            <StackLayout.BindingContext>
                <local:ViewModelA />
            </StackLayout.BindingContext>
            <Button Command ="{Binding GetMetadataCommand}"/>
        </StackLayout>
        <StackLayout BindingContext="{Binding Source={x:Reference StackLayout1}, Path=BindingContext.ViewModelB}">
            <Label Text="{Binding Metadata}" />
        </StackLayout>
    </StackLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM