在WPF前端显示C#值并保持数据持久性

rvpgvaaj  于 2023-10-22  发布在  C#
关注(0)|答案(1)|浏览(183)

我试图显示应用程序Docker Desktop是打开还是关闭,当我记录它时,我在c#中有很好的值,但在wpf中,第一个值是display,但它从未更新。我看了很多教程,但我找不到为什么它不适合我。
下面是我的XAML:

<Window x:Class="GrafanaLauncher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GrafanaLauncher"
        mc:Ignorable="d"
        Title="MainWindow" ResizeMode="NoResize" Height="450" Width="800">
    <Window.DataContext>
        <local:DockerViewModel/>
    </Window.DataContext>
    <Grid>
        <Label Content="{Binding Docker.Status}" Margin="303,110,303,268"></Label>
        <Button Margin="258,192,427,192" Content="Start Grafana" Click="ButtonStartGrafana" Background="Green"></Button>
        <Button Margin="427,192,258,192" Content="Stop Grafana" Click="ButtonStopGrafana" Background="Red"></Button>
        <Button Style="{StaticResource OpenGrafanaTemplate}" Margin="340,293,340,101"  Width="120" Height="40" BorderBrush="#fff" Name="OpenGrafanaButton"  Click="OpenGrafana">
            <Underline>Open Grafana
            </Underline>
        </Button>
    </Grid>
</Window>

下面是我的C#:

class DockerViewModel : ObservableObject
    {

        private DockerModel dockerModel;

        public event PropertyChangedEventHandler PropertyChanged;

        public DockerViewModel()
        {

            DockerUpdate();
            //Debug.WriteLine(Docker.Status);

            Task.Run(() =>
            {
                while (true)
                {

                    if (Docker == null)
                    {
                        Docker = new DockerModel("CHECKING");
                    }else
                    {
                        Debug.Write("FJKDHSGFD");
                    }

                    Thread.Sleep(1000);

                }
            });

        }

        public DockerModel Docker
        {
            get => dockerModel;
            set
            {
                if (dockerModel != value)
                {
                    dockerModel = value;
                    OnPropertyChanged(dockerModel.Status);
                }
            }
        }

        public void DockerUpdate()
        {

            Task.Run(() =>
            {
                while (true)
                {
                    Process[] processlist = Process.GetProcesses();

                    foreach (Process process in processlist)
                    {

                        if (!String.IsNullOrEmpty(process.MainWindowTitle))
                        {

                            if (process.MainWindowTitle == "Volumes - Docker Desktop")
                            {
                                dockerModel.Status = "OPEN";
                                break;
                            }
                            else
                            {
                                dockerModel.Status = "CLOSE";
                            }
                        }
                    }
                    Debug.WriteLine("DOCKER MODEL : " + Docker.Status);

                    Thread.Sleep(1000);
                }
            });
        }

    }

这里是我的类ObservableObject,它实现了INotifyPropertyChanged:

class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我试过了,但这不工作,有人有解决方案吗?
提前感谢Nicolas

vcirk6k6

vcirk6k61#

DockerModel类应该实现INotifyPropertyChanged并为数据绑定Status属性引发PropertyChanged事件:

public string Status
{
    get => status;
    set
    {
        status = value;
        OnPropertyChanged(nameof(Status));
    }
}

此外,您应该修改视图模型的Docker属性,以便为Docker属性调用OnPropertyChanged

public DockerModel Docker
{
    get => dockerModel;
    set
    {
        if (dockerModel != value)
        {
            dockerModel = value;
            OnPropertyChanged(nameof(Docker));
        }
    }
}

dockerModel.Status的值传递给OnPropertyChanged方法是没有意义的。您将传递要更新的属性的名称。

相关问题