- 嗨,我正在嘗試將 ImageButton Source 連接到 MainPage 中的一個可變字串屬性。我不確定這是否是正確的方法。但是,我試圖找到一種使用系結運算式將兩者系結在一起的方法。
namespace MyNameSpace
{
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
public new event PropertyChangedEventHandler PropertyChanged;
public String One => "MyNameSpace.Images.Blue.jpg";
public MainPage()
{
InitializeComponent();
}
protected override void OnPropertyChanged ([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
這是我創建的 MarkUpExtension,用于在使用嵌入式影像資源時提供字串作為資源。
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyNameSpace
{
[ContentProperty("ResourceId")]
public class EmbeddedImage : IMarkupExtension
{
public string ResourceId { get; set;}
public object ProvideValue(IServiceProvider serviceProvider)
{
if (String.IsNullOrWhiteSpace(ResourceId))
return null;
return ImageSource.FromResource(ResourceId);
}
}
}
這就是我試圖在 xaml 中將字串系結在一起的方式...
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyNameSpace"
x:Class="MyNameSpace.MainPage"
x:Name = "Main">
<StackLayout
BindingContext="{x:Reference Name=Main }">
<Image
Source="{local:EmbeddedImage ResourceId = {Binding One}}">
</Image>
</StackLayout>
</ContentPage>
顯然它不起作用...我收到錯誤:MainPage.xaml(13, 12): [XFC0009] 沒有找到“ResourceId”的屬性、BindableProperty 或事件,或者值和屬性之間的型別不匹配。
也許這不是這樣做的方法。我真的很感激有人“教”我,即使采用不同的方法。非常感謝。
uj5u.com熱心網友回復:
您有一個可以更改的字串,該字串表示嵌入的資源(影像)
我的建議是使用價值轉換器
首先,您必須修改字串屬性以在值更改時通知 UI
private string _one;
public string One
{
get=>_one;
set{_one=value; NotifyPropertyChanged();}
}
然后您必須為轉換器創建一個新類,這里將使用您的代碼將字串轉換為 ImageSource
public class converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string ResourceId=value.ToString();
if (String.IsNullOrWhiteSpace(ResourceId))
return null;
return ImageSource.FromResource(ResourceId);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后,就像任何其他轉換器一樣,將其添加到App.xaml. 這將取決于您的名稱類和命名空間
<converters:converter x:Key="myConverter"/>
最后,影像的代碼
<Image Source="{Binding One, Converter={StaticResource myConverter}}"/>
此外,如果您的應用程式不斷增長,請考慮將 ViewModel 分離到另一個類
快樂編碼!
uj5u.com熱心網友回復:
不需要撰寫標記擴展。
ImageSource在 xaml 中創建一個屬性并系結到該屬性。- 使用
ImageSource.FromStream方法設定屬性(并傳遞嵌入的影像流)。
看看下面的例子。
背后的代碼
using System;
using System.IO;
using System.Reflection;
using Xamarin.Forms;
namespace App5
{
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
SetImage();
}
private int Num = 1;
private static readonly Assembly Assembly = typeof(MainPage).Assembly;
private ImageSource _Source;
public ImageSource Source
{
get => _Source;
set
{
_Source = value;
OnPropertyChanged();
}
}
private void SetImage()
{
Source = ImageSource.FromStream(() => GetResourceStream($"{Num}.png"));
}
private void bChange_OnClicked(object sender, EventArgs e)
{
Num = Num == 1 ? 2 : 1;
SetImage();
}
private static Stream GetResourceStream(string filename)
=> Assembly.GetManifestResourceStream($"App5.{filename}");
}
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="App5.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="This">
<StackLayout>
<Button Text="Change" Clicked="bChange_OnClicked" />
<Image x:Name="img" Source="{Binding Source,Source={x:Reference This}}" />
</StackLayout>
</ContentPage>
為了使示例作業,您需要將專案默認命名空間設定為并在根檔案夾中App5嵌入兩個帶有檔案名的影像。1.png2.png
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313975.html
標籤:C# xml xamarin.forms 数据绑定 标记扩展
