c# - XAML 如何在主要方法之外使用图像?
问题描述
我放置了一个没有源的图像,我希望图像的源在 if 状态之间切换,如下例所示:
XAML: 在此处输入图像描述
这是我的 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));
}
}
}
我的问题是我只能在主要方法中使用图像,在它之外它无法识别它,我正在尝试这样做:
eaMafra.Source = 绿色;eaMafra.Source = 红色;
欢迎任何帮助。
解决方案
推荐阅读
- elasticsearch - Kibana 7.11 - 索引模式导出未显示任何字段
- python - 实时处理程序将新数据从吉时利数字万用表附加到 csv 文件而不覆盖数据
- python - `re.Pattern.search()` 中的 UnicodeDecodeError
- c++ - 如果 operator!=() 使用 operator==() 的否定,我应该内联运算符 == 和 !=
- python - 用 Python 去噪一张照片
- css - 我不断收到“预期大括号”和“意外令牌”错误
- pandas - 什么是最好的方法,在熊猫数据框中应用函数或 for 循环来替换空值?
- r - R 根据访问日期为每个主题创建访问编号
- r - 使用 foreach 返回多个不同的数据帧
- reactjs - Webpack-dev-server 未检测到文件更改