簡體   English   中英

如何在 MAUI 中刷新 XAML 視圖?

[英]How to refresh XAML view in MAUI?

我正在制作一個應用程序,我可以根據列表動態生成布局。

<StackLayout>
        <CollectionView x:Name="taskList">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="models:Task">
                    <VerticalStackLayout Margin="15">
                        <Entry Text="{Binding name}" IsReadOnly="True" />
                        <Entry Text="{Binding departmentsString}" IsReadOnly="True"/>
                        <HorizontalStackLayout>
                            <Entry Text="{Binding status}" IsReadOnly="True"/>
                            <Entry Text="{Binding deadline}" IsReadOnly="True" />
                            <Entry Text="{Binding author.fullName}" IsReadOnly="True"/>
                        </HorizontalStackLayout>
                        <Entry Text="{Binding description}" IsReadOnly="True" />
                    </VerticalStackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

該列表是這樣綁定的:

taskList.ItemsSource = company.tasks;

每當我向列表中添加新項目時,我都想刷新它。

我嘗試將列表重新綁定到ItemsSource ,但它不起作用:

taskList.ItemsSource = company.tasks;

我應該怎么做? 我可以只刷新視圖以便它再次生成所有內容嗎?

您應該使用一個IEnumerable集合來為 ItemsSource 發送屬性更改通知(例如ObservableCollection ),然后簡單地添加到基礎集合中。 這是一個例子:

ObservableCollection<string> tasks = new();
taskList.ItemsSource = tasks;
tasks.Add("Do something");

微軟參考

您不應從代碼隱藏中分配ItemsSource ,而應綁定一個自定義集合,這使得它變得更加容易並分離了 UI 和邏輯。 閱讀有關MVVM模式的更多信息。

(我這里沒有IDE,所以沒測試)

你可以這樣做:

// data class
public class TaskInfo
{
    public string name {get; set;}
    public string departmentsString {get;set;}
}

// you page/view whatever code behind the xaml
public class YourPage : // insert the class which it is derived from.
{
   // create a collection property, which generates events when changed. The UI react on these events.
   public ObservableCollection<TaskInfo> Tasks {get;} = new();

   public YourPage()
   {
      InitComponentsOrSomething();
      // assign the datacontext (which the view binds to)
      this.DataContext = this;
   }
}

而您的 xaml 將是這樣的:(注意綁定而不是名稱)

<StackLayout>
    <CollectionView ItemsSource="{Binding Tasks}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="models:Task">
                <VerticalStackLayout Margin="15">
                    <Entry Text="{Binding name}" IsReadOnly="True" />
                    <Entry Text="{Binding departmentsString}" IsReadOnly="True"/>
                    <HorizontalStackLayout>
                        <Entry Text="{Binding status}" IsReadOnly="True"/>
                        <Entry Text="{Binding deadline}" IsReadOnly="True" />
                        <Entry Text="{Binding author.fullName}" IsReadOnly="True"/>
                    </HorizontalStackLayout>
                    <Entry Text="{Binding description}" IsReadOnly="True" />
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

如果要將項目添加到集合中,只需使用Tasks屬性:

Tasks.Add(new TaskInfo { name = "some name", departmentsString = "Enter here" });

ps:我不會將數據 object 稱為“任務”,或者給它一個更好的描述。 任務信息/公司任務。

暫無
暫無

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

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