gpt4 book ai didi

c# - Xamarin XAML StaticResource 在另一个里面

转载 作者:行者123 更新时间:2023-11-30 21:37:45 26 4
gpt4 key购买 nike

我在 App.xaml 的 ResourceDictionary 中有一个字符串。

<x:String x:Key="logoEvo">.Images.logoEvo.png</x:String>

我怎样才能像这样在另一个 StaticResource 中使用它:

<Image Source="{sgat:ImageResource SgatMobileOnline{StaticResource logoEvo} }" />

ImageResource 的文件是:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

namespace SgatMobileOnline {

// You exclude the 'Extension' suffix when using in Xaml markup
[Preserve(AllMembers = true)]
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension {
public string Source { get; set; }

public object ProvideValue(IServiceProvider serviceProvider) {
if (Source == null) {
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source);

return imageSource;
}
}
}

谢谢!

最佳答案

在 App.cs 中

 <Application.Resources>
<ResourceDictionary>
<x:String x:Key="logoEvo">test.png</x:String>
</ResourceDictionary>
</Application.Resources>

XAML:

<Image Source="{local:ImageResourceExtension logoEvo}" />

图像资源扩展

namespace App1
{
[Preserve(AllMembers = true)]
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }

public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}

string s = Application.Current.Resources[Source] as string;
// Do your translation lookup here, using whatever method you require

var assembly = typeof(ImageResourceExtension).GetTypeInfo().Assembly;
var assemblyString = assembly.GetName().Name.ToString();
var imageSource = ImageSource.FromResource(assemblyString + "." + s);
return imageSource;
}
}
}

PS:确保图片的Build Action为Embedded Resource

关于c# - Xamarin XAML StaticResource 在另一个里面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46936824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com