【发布时间】:2021-06-16 04:07:56
【问题描述】:
我正在使用 Xamarin 开发应用程序
基本上,管理员用户应该能够访问此按钮。我以前用过这个逻辑,效果很好。
普通用户不应有权隐藏其他普通用户的评论。
有人告诉我,它嵌套在轮播视图中存在问题。
我可以重组整个东西,但我喜欢目前使用的设计
XAML 代码如下:
<?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="GOV.ReviewPage" Title="Reviews" >
<Grid RowDefinitions="20,180,20,32,230,90" ColumnDefinitions="20,*,20">
<Frame Grid.Row="1" Grid.Column="1" Grid.RowSpan="2">
<CarouselView x:Name="MainCarousel" IndicatorView="indicatorView" IsBounceEnabled="True" >
<CarouselView.ItemTemplate>
<DataTemplate>
<ScrollView>
<Grid RowDefinitions="20,20,*" ColumnDefinitions="75,*" Margin="15,0,25,0">
<Label Text="Username:" Grid.Row="0" Grid.Column="0" HorizontalTextAlignment="Start" />
<Label Text="{Binding User.Username}" FontSize="15" Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="Start" />
<Label Text="Description:" Grid.Row="1" Grid.Column="0" HorizontalTextAlignment="Start" />
<Label Text="{Binding Description}" FontSize="15" HorizontalTextAlignment="Start" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"/>
<Frame Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" >
<Button x:Name="HideButtonRef" Clicked="HideButton" Text="{Binding stringVal}"
HeightRequest="50" />
</Frame>
</Grid>
</ScrollView>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Frame>
</Grid>
</ContentPage>
还有问题的 C# 逻辑:
protected override async void OnAppearing()
{
if (User.Admin) { HideButtonRef.IsEnabled = false; } // this doesnt work for some reason
else {HideButtonRef.IsEnabled = true; }
base.OnAppearing();
await LoadList();
}
c# 逻辑说“名称‘HideButtonRef’在当前上下文中不存在”
【问题讨论】:
-
这样的问题有几十个。模板中的元素不能按名称引用。使用数据绑定来设置它的属性。
标签: c# xamarin xamarin.forms binding