【问题标题】:Xamarin MainPage error: name doesn't exist in the current contextXamarin MainPage 错误:当前上下文中不存在名称
【发布时间】:2020-07-21 13:15:43
【问题描述】:

我的 MainPage.xml 中有一个名为“txtInhoud”的标签,但是当我尝试在 MainPage.xml.cs (txtInhoud.Text = [...]) 中设置它的文本时,我得到了当前上下文中不存在该名称的错误。

我尝试重建项目并删除 bin 和 obj 文件,但没有成功。

有什么帮助吗?

【问题讨论】:

  • Label 在模板内吗?
  • 你这是什么意思?它在github.com/markolazic88/SwipeCardView的swipeCardView里面
  • Label显然DataTemplate 内。您不能按名称引用模板内的项目。您应该使用数据绑定来设置模板元素的属性。

标签: c# visual-studio xamarin xamarin.forms


【解决方案1】:

从示例代码中,Label 位于 DataTemplate 内部。我们不能 getset Text by x:Name = "txtInhoud" ,因为它已经与 ItemsSource 绑定。

<swipeCardView:SwipeCardView
    ItemsSource="{Binding CardItems}"
    SwipedCommand="{Binding SwipedCommand}"
    LoopCards="{Binding IsLoopCards}"
    VerticalOptions="FillAndExpand">
    <swipeCardView:SwipeCardView.ItemTemplate>
        <DataTemplate>
            <Label x:Name="txtInhoud" Text="{Binding .}" FontSize="Large" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="Beige" />
        </DataTemplate>
    </swipeCardView:SwipeCardView.ItemTemplate>
</swipeCardView:SwipeCardView>

可以看到swipeCardView:SwipeCardView绑定了ItemsSource="{Binding CardItems}",这个ContentPage绑定了BindingContext = new SimplePageViewModel();

那么Label的文字就是从SimplePageViewModel设计的。

public class SimplePageViewModel : BasePageViewModel
{
    private ObservableCollection<string> _cardItems;
    private bool _isLoopCards;
    private string _message;

    public SimplePageViewModel()
    {
        _cardItems = new ObservableCollection<string>();
        for (var i = 1; i <= 5; i++)
        {
            _cardItems.Add($"Card {i}");
        }
        _isLoopCards = true;
        SwipedCommand = new Command<SwipedCardEventArgs>(OnSwipedCommand);

        ClearItemsCommand = new Command(OnClearItemsCommand);
        AddItemsCommand = new Command(OnAddItemsCommand);
    }

    public ObservableCollection<string> CardItems
    {
        get => _cardItems;
        set
        {
            _cardItems = value;
            RaisePropertyChanged();
        }
    }
...
}

从上面的代码中,你会看到CardItems添加了五个string元素。由于LabelText="{Binding .}"绑定文本,那么每个LabelText就是CardItems的每个string元素。

此外,我们可以为 ViewModel 中的每个 Item 添加自定义的 Name 属性。例如修改CardItems如下:

 ...
    private ObservableCollection<CardItem> _cardItems;
    private bool _isLoopCards;
    private string _message;

    public SimplePageViewModel()
    {
        _cardItems = new ObservableCollection<CardItem>();
        for (var i = 1; i <= 5; i++)
        {
            _cardItems.Add(new CardItem() { Name = $"Custom Card {i}" });
        }

        _cardItems[0].Name = "This is the first Card Item";

        _isLoopCards = true;
        SwipedCommand = new Command<SwipedCardEventArgs>(OnSwipedCommand);

        ClearItemsCommand = new Command(OnClearItemsCommand);
        AddItemsCommand = new Command(OnAddItemsCommand);
    }
 ...

CardItem 定义如下:

public class CardItem
{
    public string Name { set; get; }
}

Xaml还需要修改Text绑定键为:&lt;Label Text="{Binding Name}" ...

现在效果:

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多