XAML如何在主要方法之外使用图像?

时间:2018-12-12 10:42:34

标签: c# wpf visual-studio

我放置了一个没有源的图像,我希望图像的源在以下示例的状态之间切换:

XAML: enter image description here

这是我的C#代码:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var startTimeSpan = TimeSpan.Zero; //Start Counting from 0
        var periodTimeSpan = TimeSpan.FromMinutes(10); //Every 10 minutes
        var timer = new System.Threading.Timer((e) =>
        {
            EA(); //loads the method every 10 minute

        }, null, startTimeSpan, periodTimeSpan);
    }

    public static void EA()
    {

        var lines = File.ReadAllLines("EscolaDasArmas.txt"); //reads ip cameras txt
        int timeout = 0; //Count how many timeout
        int success = 0; //Count how many success
        for (int i = 0; i < lines.Length; i++) //for each line in txt 
        {
            Ping ping = new Ping();
            PingReply reply = ping.Send(lines[i]); //ping each camera
            if (reply.Status == IPStatus.Success)
            {
                success++; //Increments success cameras
            }

            else if (reply.Status == IPStatus.TimedOut)
            {
                timeout++; //Increments for each timeout camera
            }

        }
        if (success > timeout)
        {
            PingStatusGreen();
            //loads green image
        }

        else
        {
            PingStatusRed();
            //loads red image
        }
    }

    public static void PingStatusGreen()
    {
        BitmapImage green = new BitmapImage(new Uri("/CameraControl;component/status/green.png", UriKind.Relative)); 

    }

    public static void PingStatusRed()
    {
        BitmapImage red = new BitmapImage(new Uri("/CameraControl;component/status/red.png", UriKind.Relative));
    }
}

}

我的问题是我只能在main方法内部使用图像,而在外部方法无法识别图像时,我正在尝试这样做:

eaMafra.Source =绿色; eaMafra.Source =红色;

欢迎任何帮助。

0 个答案:

没有答案