英文:
.NET MAUI HorizontalStackLayout Trigger not triggers
问题
以下是代码部分的翻译:
我想创建一个类似于向导的东西来拍照。我在网格中垂直地放置了4个HorizontalStackLayout组件。在每一步之后,我想隐藏前一个并显示下一个。
这是我遇到问题的代码:
<Grid RowDefinitions="30,40" ColumnDefinitions="5*,1*,1*">
<VerticalStackLayout>
<HorizontalStackLayout x:Name="houseSideSptepOne">
<HorizontalStackLayout.Triggers>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="1">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</HorizontalStackLayout.Triggers>
<Label Grid.Row="0" Grid.Column="0"
Text="{loc:Localize HouseSide_1}"
FontFamily="PoppinsSemibold" TextColor="{StaticResource Gray600}" FontSize="17"
HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="1"
Command="{Binding CameraCommand}" CommandParameter="1"
Source="camera.png" HeightRequest="30"
WidthRequest="30" Margin="30,0,30,0"
HorizontalOptions="End" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="2"
Command="{Binding SideViewImagesGalleryCommand}" CommandParameter="1"
Source="gallery.png" HeightRequest="30"
WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center" />
</HorizontalStackLayout>
<!-- 后续的 HorizontalStackLayout 也类似,省略部分内容 -->
</VerticalStackLayout>
<ImageButton Source="checkmark.png" WidthRequest="30"
Grid.Row="1" Grid.Column="2"
HorizontalOptions="End"
Command="{Binding NextSideViewCommand}"/>
</Grid>
请注意,这是您提供的代码的翻译部分,不包括代码的其他部分。如果您需要进一步的帮助或有其他问题,请随时提出。
英文:
I wanted to create something like a wizard for taking images. I put in the grid vertically 4 HorizontalStackLayout components. After a step a I want to hide the previous and show the next one.
Here is a code, that I am struggling with:
<Grid RowDefinitions="30,40" ColumnDefinitions="5*,1*,1*">
<VerticalStackLayout>
<HorizontalStackLayout x:Name="houseSideSptepOne">
<HorizontalStackLayout.Triggers>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="1">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</HorizontalStackLayout.Triggers>
<Label Grid.Row="0" Grid.Column="0"
Text="{loc:Localize HouseSide_1}"
FontFamily="PoppinsSemibold" TextColor="{StaticResource Gray600}" FontSize="17"
HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="1"
Command="{Binding CameraCommand}" CommandParameter="1"
Source="camera.png" HeightRequest="30"
WidthRequest="30" Margin="30,0,30,0"
HorizontalOptions="End" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="2"
Command="{Binding SideViewImagesGalleryCommand}" CommandParameter="1"
Source="gallery.png" HeightRequest="30"
WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center" />
</HorizontalStackLayout>
<HorizontalStackLayout x:Name="houseSideSptepTwo">
<HorizontalStackLayout.Triggers>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="2">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</HorizontalStackLayout.Triggers>
<Label Grid.Row="0" Grid.Column="0"
Text="{loc:Localize HouseSide_2}"
FontFamily="PoppinsSemibold" TextColor="{StaticResource Gray600}" FontSize="17"
HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="1"
Command="{Binding CameraCommand}" CommandParameter="2"
Source="camera.png" HeightRequest="30"
WidthRequest="30" Margin="30,0,30,0"
HorizontalOptions="End" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="2"
Command="{Binding SideViewImagesGalleryCommand}" CommandParameter="2"
Source="gallery.png" HeightRequest="30"
WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center" />
</HorizontalStackLayout>
<HorizontalStackLayout x:Name="houseSideSptepThree">
<HorizontalStackLayout.Triggers>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="3">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</HorizontalStackLayout.Triggers>
<Label Grid.Row="0" Grid.Column="0"
Text="{loc:Localize HouseSide_3}"
FontFamily="PoppinsSemibold" TextColor="{StaticResource Gray600}" FontSize="17"
HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="1"
Command="{Binding CameraCommand}" CommandParameter="3"
Source="camera.png" HeightRequest="30"
WidthRequest="30" Margin="30,0,30,0"
HorizontalOptions="End" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="2"
Command="{Binding SideViewImagesGalleryCommand}" CommandParameter="3"
Source="gallery.png" HeightRequest="30"
WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center" />
</HorizontalStackLayout>
<HorizontalStackLayout x:Name="houseSideSptepFour">
<HorizontalStackLayout.Triggers>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="4">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</HorizontalStackLayout.Triggers>
<Label Grid.Row="0" Grid.Column="0"
Text="{loc:Localize HouseSide_4}"
FontFamily="PoppinsSemibold" TextColor="{StaticResource Gray600}" FontSize="17"
HorizontalOptions="FillAndExpand" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="1"
Command="{Binding CameraCommand}" CommandParameter="4"
Source="camera.png" HeightRequest="30"
WidthRequest="30" Margin="30,0,30,0"
HorizontalOptions="End" VerticalOptions="Center" />
<ImageButton Grid.Row="0" Grid.Column="2"
Command="{Binding SideViewImagesGalleryCommand}" CommandParameter="4"
Source="gallery.png" HeightRequest="30"
WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center" />
</HorizontalStackLayout>
</VerticalStackLayout>
<ImageButton Source="checkmark.png" WidthRequest="30"
Grid.Row="1" Grid.Column="2"
HorizontalOptions="End"
Command="{Binding NextSideViewCommand}"/>
</Grid>
In the view model I added a property:
[ObservableProperty]
private int houseSideSptep = 1;
Created a command
public IRelayCommand NextSideViewCommand => new RelayCommand(ExecuteNextSideViewCommand);
private void ExecuteNextSideViewCommand() => HouseSideSptep++
;
In the view's code behind I hided the last three HorizontalStackLayouts.
public DetailedDataPage(DetailedDataViewModel DetailedDataViewModel)
{
InitializeComponent();
this.BindingContext = DetailedDataViewModel;
houseSideSptepTwo.IsVisible = false;
houseSideSptepThree.IsVisible = false;
houseSideSptepFour.IsVisible = false;
}
In debug I can see that the button click hits the command and the HouseSideStep value increments, but on the UI no changes.
Previous I did this in code, and that worked from the button event and setting the visible property of each HorizontalStackLayout.
thnx
答案1
得分: 1
以下是您要翻译的内容:
"可能与 DataTrigger 以及在页面首次绘制时具有 IsVisible=False 的视图相关的 Maui bug。作为测试,请尝试以下内容。它将首先显示它们(我知道您不希望这样),但如果它有效,可以帮助隔离问题:
public DetailedDataPage(DetailedDataViewModel DetailedDataViewModel)
{
InitializeComponent();
this.BindingContext = DetailedDataViewModel;
// --- 删除此行。从 #2 可见开始 ---
//houseSideSptepTwo.IsVisible = false;
}
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="1">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="2">
<Setter Property="IsVisible" Value="False" />
</DataTrigger>
...
... #2 上有类似的逻辑,但值为 "2" => True,"3" => false ...
```xaml
测试的想法是看看一个最初可见的元素是否可以隐藏。
如果这起作用,那么您的原始问题肯定是一个涉及 IsVisible 和 DataTrigger 的 Maui bug。
如果它起作用,那么通过适当的触发器,可以绕过此 bug:
1. 从所有内容可见开始。
2. 从 `houseSideSptep = 0;` 开始,我们将在步骤 4 中强制更改。
3. 添加 DataTriggers,当 HouseSideSptep 为 1 时隐藏 #2、#3、#4。
4. 在构造函数末尾添加:
```cs
Dispatcher.Dispatch( async() =>
{
await Task.Delay(500); // HACK,以确保页面已显示。待定更好的在页面显示后运行代码的方法。
HouseSideSptep = 1;
});
"
英文:
There might be a Maui bug related to DataTrigger and a view that has IsVisible=False when the page is first drawn.
As a test, try this. It will show them all at first (which I know you don't want), but if it works, that helps isolate the problem:
public DetailedDataPage(DetailedDataViewModel DetailedDataViewModel)
{
InitializeComponent();
this.BindingContext = DetailedDataViewModel;
// --- remove this. Start with #2 visible ---
//houseSideSptepTwo.IsVisible = false;
}
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="1">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
<DataTrigger TargetType="HorizontalStackLayout"
Binding="{Binding HouseSideSptep}"
Value="2">
<Setter Property="IsVisible" Value="False" />
</DataTrigger>
...
... similar logic on #2, but with values "2" => True, "3" => false ...
```xaml
The idea of the test is to see if an element that starts visible, can be made to hide.
If this works, then your original problem is definitely a Maui bug, involving IsVisible and DataTrigger.
If it works, then with appropriate triggers, the bug can be worked around:
1. Start with everything visible.
2. Start with `houseSideSptep = 0;`, which we will force to change in step 4.
3. Add DataTriggers that hide #2, #3, #4, when HouseSideSptep is 1.
4. At end of constructor, add:
```cs
Dispatcher.Dispatch( async() =>
{
await Task.Delay(500); // HACK to make sure page has displayed. TBD a better way to run code after a page is displayed.
HouseSideSptep = 1;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论