![](/img/trans.png)
[英]How to load an XML file from a file in the solution, build with Embedded Resource? Project > Cross Platform > Xamarin.Forms
[英]How do I create an object of an Android Project PageRenderer class in my main solution in a cross platform xamarin project?
我的起始页上有一张背景图片,当应用程序运行时,该图片会被拉伸以适应屏幕。 我试图避免这种拉伸,因为它看起来很糟糕。
我在 android 项目中创建了一个页面渲染器 class:
[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]
namespace ChoiceBasedStoryGame.Droid
{
public class CustomPageRenderer : PageRenderer
{
public CustomPageRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
var activity = this.Context as Activity;
activity.Window.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.cyberpunk_hacker));
var imageView = activity.FindViewById<ImageView>(Android.Resource.Id.Content);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
}
}
}
我想在我的起始页中引用它:
namespace ChoiceBasedStoryGame
{
public class StartPage : ContentPage
{
public StartPage()
{
var renderer = new CustomPageRenderer(this.Context);
this.BackgroundImageSource = "cyberpunk_hacker.png";
this.Renderer = new CustomPageRenderer(this.Context);
我收到命名空间“CustomPageRenderer”不存在的错误。
但这会导致与“Droid”类似的错误。 我验证了 android 属性中的引用是正确的。
我试过在顶部添加“使用”:
using ChoiceBasedStoryGame.Droid;
有没有更简单的方法可以将背景图像的比例设置为不拉伸?
谢谢你。
首先,如果您只想对 StartPage 使用自定义渲染器,可以将[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]
更改为[assembly: ExportRenderer(typeof(StartPage), typeof(CustomPageRenderer))]
。
您可以查看有关自定义内容页面的官方文档以获取更多详细信息。
此外,关于如何适当设置页面的背景图片,还有很多其他的方法。 比如你可以像我以前的回答那样使用网格中的图像控件。 您还可以参考这个案例,关于在 Xamarin.Forms 中更改背景图片的纵横比,同时保持内容居中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.