.NET MAUI HorizontalStackLayout 触发器未触发。

huangapple go评论66阅读模式
英文:

.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:

            &lt;Grid RowDefinitions=&quot;30,40&quot; ColumnDefinitions=&quot;5*,1*,1*&quot;&gt;
&lt;VerticalStackLayout&gt;
&lt;HorizontalStackLayout x:Name=&quot;houseSideSptepOne&quot;&gt;
&lt;HorizontalStackLayout.Triggers&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot;
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;1&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;/HorizontalStackLayout.Triggers&gt;
&lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
Text=&quot;{loc:Localize HouseSide_1}&quot;
FontFamily=&quot;PoppinsSemibold&quot; TextColor=&quot;{StaticResource Gray600}&quot; FontSize=&quot;17&quot;
HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot;
Command=&quot;{Binding CameraCommand}&quot; CommandParameter=&quot;1&quot; 
Source=&quot;camera.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot; Margin=&quot;30,0,30,0&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot;
Command=&quot;{Binding SideViewImagesGalleryCommand}&quot; CommandParameter=&quot;1&quot; 
Source=&quot;gallery.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;/HorizontalStackLayout&gt;
&lt;HorizontalStackLayout x:Name=&quot;houseSideSptepTwo&quot;&gt;
&lt;HorizontalStackLayout.Triggers&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot; 
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;2&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;/HorizontalStackLayout.Triggers&gt;
&lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
Text=&quot;{loc:Localize HouseSide_2}&quot;
FontFamily=&quot;PoppinsSemibold&quot; TextColor=&quot;{StaticResource Gray600}&quot; FontSize=&quot;17&quot;
HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot;
Command=&quot;{Binding CameraCommand}&quot; CommandParameter=&quot;2&quot; 
Source=&quot;camera.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot; Margin=&quot;30,0,30,0&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot;
Command=&quot;{Binding SideViewImagesGalleryCommand}&quot; CommandParameter=&quot;2&quot; 
Source=&quot;gallery.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;/HorizontalStackLayout&gt;
&lt;HorizontalStackLayout x:Name=&quot;houseSideSptepThree&quot;&gt;
&lt;HorizontalStackLayout.Triggers&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot; 
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;3&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;/HorizontalStackLayout.Triggers&gt;
&lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
Text=&quot;{loc:Localize HouseSide_3}&quot;
FontFamily=&quot;PoppinsSemibold&quot; TextColor=&quot;{StaticResource Gray600}&quot; FontSize=&quot;17&quot;
HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot;
Command=&quot;{Binding CameraCommand}&quot; CommandParameter=&quot;3&quot; 
Source=&quot;camera.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot; Margin=&quot;30,0,30,0&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot;
Command=&quot;{Binding SideViewImagesGalleryCommand}&quot; CommandParameter=&quot;3&quot; 
Source=&quot;gallery.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;/HorizontalStackLayout&gt;
&lt;HorizontalStackLayout x:Name=&quot;houseSideSptepFour&quot;&gt;
&lt;HorizontalStackLayout.Triggers&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot; 
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;4&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;/HorizontalStackLayout.Triggers&gt;
&lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot;
Text=&quot;{loc:Localize HouseSide_4}&quot;
FontFamily=&quot;PoppinsSemibold&quot; TextColor=&quot;{StaticResource Gray600}&quot; FontSize=&quot;17&quot;
HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot;
Command=&quot;{Binding CameraCommand}&quot; CommandParameter=&quot;4&quot; 
Source=&quot;camera.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot; Margin=&quot;30,0,30,0&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;ImageButton Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot;
Command=&quot;{Binding SideViewImagesGalleryCommand}&quot; CommandParameter=&quot;4&quot; 
Source=&quot;gallery.png&quot; HeightRequest=&quot;30&quot; 
WidthRequest=&quot;30&quot;
HorizontalOptions=&quot;End&quot; VerticalOptions=&quot;Center&quot; /&gt;
&lt;/HorizontalStackLayout&gt;
&lt;/VerticalStackLayout&gt;
&lt;ImageButton Source=&quot;checkmark.png&quot; WidthRequest=&quot;30&quot;
Grid.Row=&quot;1&quot; Grid.Column=&quot;2&quot;
HorizontalOptions=&quot;End&quot;
Command=&quot;{Binding NextSideViewCommand}&quot;/&gt;
&lt;/Grid&gt;

In the view model I added a property:

[ObservableProperty]
private int houseSideSptep = 1;

Created a command

public IRelayCommand NextSideViewCommand =&gt; new RelayCommand(ExecuteNextSideViewCommand);
private void ExecuteNextSideViewCommand() =&gt; 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;
}
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot;
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;1&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot;
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;2&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;False&quot; /&gt;
&lt;/DataTrigger&gt;
...
... #2 上有类似的逻辑,但值为 &quot;2&quot; =&gt; True,&quot;3&quot; =&gt; false ...
```xaml
测试的想法是看看一个最初可见的元素是否可以隐藏。
如果这起作用,那么您的原始问题肯定是一个涉及 IsVisible 和 DataTrigger 的 Maui bug。
如果它起作用,那么通过适当的触发器,可以绕过此 bug:
1. 从所有内容可见开始。
2. 从 `houseSideSptep = 0;` 开始,我们将在步骤 4 中强制更改。
3. 添加 DataTriggers,当 HouseSideSptep 为 1 时隐藏 #2、#3、#4。
4. 在构造函数末尾添加:
```cs
Dispatcher.Dispatch( async() =&gt;
{
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;
}
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot;
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;1&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;True&quot; /&gt;
&lt;/DataTrigger&gt;
&lt;DataTrigger TargetType=&quot;HorizontalStackLayout&quot;
Binding=&quot;{Binding HouseSideSptep}&quot;
Value=&quot;2&quot;&gt;
&lt;Setter Property=&quot;IsVisible&quot; Value=&quot;False&quot; /&gt;
&lt;/DataTrigger&gt;
...
... similar logic on #2, but with values &quot;2&quot; =&gt; True, &quot;3&quot; =&gt; 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() =&gt;
{
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;
});

huangapple
  • 本文由 发表于 2023年5月14日 07:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定