33 lines
1.0 KiB
C#
33 lines
1.0 KiB
C#
|
using System;
|
|||
|
using Avalonia;
|
|||
|
using Avalonia.Controls;
|
|||
|
using Avalonia.Markup.Xaml;
|
|||
|
using Avalonia.Media;
|
|||
|
using Avalonia.Media.Imaging;
|
|||
|
using Avalonia.Platform;
|
|||
|
|
|||
|
namespace ZTools.Views;
|
|||
|
|
|||
|
public partial class TestWindow : Window
|
|||
|
{
|
|||
|
public TestWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
|
|||
|
// found1 = false | result1 = null
|
|||
|
var found1 = this.TryGetResource("TheKey", this.ActualThemeVariant, out var result1);
|
|||
|
|
|||
|
// found2 = true | result2 = "Hello World"
|
|||
|
var found2 = this.TryFindResource("TheKey", this.ActualThemeVariant, out var result2);
|
|||
|
|
|||
|
// 从代码中找到资源并将其绑定到 TextBlock
|
|||
|
myTextBlock.Bind(TextBlock.TextProperty, Resources.GetResourceObservable("TheKey"));
|
|||
|
|
|||
|
var uri = "avares://ZTools/Assets/Menu/其他.png";
|
|||
|
var bitmap = new Bitmap(AssetLoader.Open(new Uri(uri)));
|
|||
|
img.Source=bitmap;
|
|||
|
|
|||
|
// 通过绑定的 observable 更新 myTextBlock.Text
|
|||
|
this.Resources["TheKey"] = "Hello from code behind";
|
|||
|
}
|
|||
|
}
|