制作按钮 .xaml public 到 wpf 控制器中的另一个 class

making button .xaml public to another class in wpf controller

我在 Main 有这个按钮 window.xaml 需要从另一个 class 调用它 我让它就像第一个答案 How to make a control in XAML public in order to be seen in other classes

 <Window x:Class="USD.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="450" Width="525"> 

<Button  ToolTip="Answer Call" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="107,82,0,0"  x:Name="answerCall"  Click="answerCall_Click"  Visibility="Hidden" x:FieldModifier="public">
        <StackPanel Orientation="Horizontal" >
            <Image  Width="35" Height="35" Source="Images/unnamed.png" RenderTransformOrigin="4.075,0.607">
            </Image>
        </StackPanel>
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <ContentPresenter HorizontalAlignment="Center"
                              VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>

但是当我从任何 .cs 检查它时 class 就像那样

MainWindow.answerCall.Visibility.Visible;

给出错误

如果您使用 controller/viewmodel,您不应该有任何代码,相反,您应该使用 bindable properties and commands, implementing INotifyPropertyChanged interface,如果您愿意,可以在某些基础 class 中使用。

我用 C# 代码修复了它

 DispatcherPriority.Background,
                    new Action(() => ((MainWindow)System.Windows.Application.Current.MainWindow).answerCall.Visibility = Visibility.Visible));