0

我的 CollectionView 中有一个标签,我需要用填充视图的 ItemsSource 列表之外的值填充它。以下代码是我要完成的示例,但似乎 CollectionView 将绑定上下文限制为仅 Items 列表。我尝试命名标签并将其设置在我的 c# 代码中,但我似乎无法访问 c# 中的标签。我想我可以在 c# 中构建整个页面,而不是使用 .xaml,但与此示例不同的是,我的实际代码使用多个模板和一个模板选择器。如果我能在不重写数小时代码的情况下解决这个问题,我会更喜欢它。

项目页面.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TabTest.Views.ItemsPage"
             Title="{Binding Title}"
             x:Name="BrowseItemsPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="AddItem_Clicked" />
    </ContentPage.ToolbarItems>
    <StackLayout Padding="10">
        <Label Text="{Binding TestVal}" FontSize="16" HeightRequest="20" />
        <!-- ^^^^ This label displays just as expected -->
        <RefreshView IsRefreshing="{Binding IsBusy, Mode=TwoWay}" Command="{Binding LoadItemsCommand}">
            <CollectionView x:Name="ItemsCollectionView"
                ItemsSource="{Binding Items}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout Padding="10">
                            <Label x:Name="TestV" Text="{Binding Path=BindingContext.TestVal}" />
                            <!-- ^^^^ I want this Label to display the TestVal string in the ViewModel -->
                            <Label Text="{Binding Text}" 
                                d:Text="{Binding .}"
                                LineBreakMode="NoWrap" 
                                Style="{DynamicResource ListItemTextStyle}" 
                                FontSize="16" />
                            <Label Text="{Binding Description}" 
                                d:Text="Item descripton"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13" />
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnItemSelected"></TapGestureRecognizer>
                            </StackLayout.GestureRecognizers>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </RefreshView>
    </StackLayout>
</ContentPage>

ItemsViewModel.cs

namespace TabTest.ViewModels
{
    public class ItemsViewModel : BaseViewModel
    {
        public ObservableCollection<Item> Items { get; set; }
        public Command LoadItemsCommand { get; set; }

        private string testVal;     
        public string TestVal       // I want the value of this variable in that Label
        {
            get
            {
                return testVal;
            }
            set
            {
                testVal = value;
            }
        }

        public ItemsViewModel()
        {
            Title = "Browse";
            TestVal = "Value123";

            Items = new ObservableCollection<Item>();
            LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());


            MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
            {
                var newItem = item as Item;
                Items.Add(newItem);
                await DataStore.AddItemAsync(newItem);
            });
        }

        async Task ExecuteLoadItemsCommand()
        {
            IsBusy = true;

            try
            {
                Items.Clear();
                var items = await DataStore.GetItemsAsync(true);
                foreach (var item in items)
                {
                    Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}
4

1 回答 1

0

我最终在 Xaml 中使用了一个动态资源,并在需要更改时使用后面的代码来修改资源。

xml:

<Label x:Name="TestV" Text="{DynamicResource TestValue}" />

代码背后:

Application.Current.Resources["TestValue"] = NewValue;

应用程序.xaml:

<x:String x:Key="TestValue">Value123</x:String>
于 2020-07-13T00:15:50.400 回答