C# 如何从Xamarin.Forms中的CodeBehind访问自定义StackLayout?
我在我的Xamarin.Forms项目中实现了一个自定义StackLayout,以使ContentPage的背景成为渐变,并且我希望根据设备的操作系统更改代码中的渐变流。问题是,当我给x:Name赋值时,我在xaml.cs类中找不到它,因此我无法访问它的属性。代码如下: 自定义堆栈布局代码:C# 如何从Xamarin.Forms中的CodeBehind访问自定义StackLayout?,c#,visual-studio,xaml,xamarin,xamarin.forms,C#,Visual Studio,Xaml,Xamarin,Xamarin.forms,我在我的Xamarin.Forms项目中实现了一个自定义StackLayout,以使ContentPage的背景成为渐变,并且我希望根据设备的操作系统更改代码中的渐变流。问题是,当我给x:Name赋值时,我在xaml.cs类中找不到它,因此我无法访问它的属性。代码如下: 自定义堆栈布局代码: namespace MyProject.Renderers { public class GradientLayout : StackLayout { public stri
namespace MyProject.Renderers
{
public class GradientLayout : StackLayout
{
public string ColorsList { get; set; }
public Color[] Colors
{
get
{
string[] hex = ColorsList.Split(',');
Color[] colors = new Color[hex.Length];
for (int i = 0; i < hex.Length; i++)
{
colors[i] = Color.FromHex(hex[i].Trim());
}
return colors;
}
}
public GradientColorStackMode Mode { get; set; }
}
}
也就是说,如果您需要更多信息,我会在看到您的请求后立即提供,谢谢大家,祝您愉快。您是否像此线程一样为IOS和Android创建了自定义渲染器 如果是这样,请确保为您的
渐变布局添加WidthRequest=“200”HeightRequest=“200”
<renderers:GradientLayout
ColorsList="#dd8f68,#a9a9a9,#3a3939"
Mode="ToBottomRight"
WidthRequest="200"
HeightRequest="200">
</renderers:GradientLayout>
下面是跑步截图
如果上述代码仍然不起作用。您可以在应用程序中使用Magic Gradients
这应该可以。对于奇怪的XAML问题,请尝试杀死VS,删除所有bin/obj文件夹,重新启动VS,并重新生成我添加的widthRequest和HeightRequest,正如您所说,现在它可以工作了,非常感谢。如果这不是太多的要求,如果你有空余时间,请你向我解释一下赋予这些财产价值有什么区别。祝您有个美好的一天。
public partial class LogInPage : ContentPage
{
public LogInPage()
{
InitializeComponent();
if (Device.RuntimePlatform == Device.iOS)
{
//theGradient do not appear
}
}
}
<renderers:GradientLayout
ColorsList="#dd8f68,#a9a9a9,#3a3939"
Mode="ToBottomRight"
WidthRequest="200"
HeightRequest="200">
</renderers:GradientLayout>