Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Xamarin forms macos中的条目(用于聊天)中添加自定义图像?_C#_Macos_Xamarin_Xamarin.forms - Fatal编程技术网

C# 如何在Xamarin forms macos中的条目(用于聊天)中添加自定义图像?

C# 如何在Xamarin forms macos中的条目(用于聊天)中添加自定义图像?,c#,macos,xamarin,xamarin.forms,C#,Macos,Xamarin,Xamarin.forms,我正在尝试创建一个条目,它支持添加文本和图像(自定义图像)。现在我正试图在macOS中实现这一点 到目前为止,我所做的是创建一个自定义控件,在其中我有一个图像列表,如果向条目中添加了新图像,我将添加到该列表中。添加后,其想法是将其放在最后一个文本(或图像)的右侧 控制: public class ChatEntry : Entry { public ChatEntry() { this.HeightRequest = 50; } public s

我正在尝试创建一个条目,它支持添加文本和图像(自定义图像)。现在我正试图在macOS中实现这一点

到目前为止,我所做的是创建一个自定义控件,在其中我有一个图像列表,如果向条目中添加了新图像,我将添加到该列表中。添加后,其想法是将其放在最后一个文本(或图像)的右侧

控制:

public class ChatEntry : Entry
{
    public ChatEntry()
    {
        this.HeightRequest = 50;
    }

    public static readonly BindableProperty ImageProperty =
        BindableProperty.Create(nameof(Images), typeof(List<string>), typeof(ChatEntry), string.Empty);

    public static readonly BindableProperty LineColorProperty =
        BindableProperty.Create(nameof(LineColor), typeof(Xamarin.Forms.Color), typeof(ChatEntry), Color.White);

    public static readonly BindableProperty ImageHeightProperty =
        BindableProperty.Create(nameof(ImageHeight), typeof(int), typeof(ChatEntry), 40);

    public static readonly BindableProperty ImageWidthProperty =
        BindableProperty.Create(nameof(ImageWidth), typeof(int), typeof(ChatEntry), 40);

    public Color LineColor
    {
        get { return (Color)GetValue(LineColorProperty); }
        set { SetValue(LineColorProperty, value); }
    }

    public int ImageWidth
    {
        get { return (int)GetValue(ImageWidthProperty); }
        set { SetValue(ImageWidthProperty, value); }
    }

    public int ImageHeight
    {
        get { return (int)GetValue(ImageHeightProperty); }
        set { SetValue(ImageHeightProperty, value); }
    }

    public List<string> Images
    {
        get { return (List<string>)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }
}
公共类聊天记录条目:条目
{
公共聊天室入口()
{
此.HeightRequest=50;
}
公共静态只读BindableProperty ImageProperty=
Create(nameof(Images)、typeof(List)、typeof(ChatEntry)、string.Empty);
公共静态只读BindableProperty LineColorProperty=
创建(nameof(LineColor)、typeof(Xamarin.Forms.Color)、typeof(ChatEntry)、Color.White);
公共静态只读BindableProperty ImageHeightProperty=
创建(nameof(ImageHeight)、typeof(int)、typeof(ChatEntry)、40);
公共静态只读BindableProperty ImageWidthProperty=
创建(nameof(ImageWidth)、typeof(int)、typeof(ChatEntry)、40);
公共颜色线条颜色
{
获取{return(Color)GetValue(LineColorProperty);}
set{SetValue(LineColorProperty,value);}
}
公共整数图像宽度
{
获取{return(int)GetValue(ImageWidthProperty);}
set{SetValue(ImageWidthProperty,value);}
}
公共int图像高度
{
获取{return(int)GetValue(ImageHeightProperty);}
set{SetValue(ImageHeightProperty,value);}
}
公共列表图像
{
获取{return(List)GetValue(ImageProperty);}
set{SetValue(ImageProperty,value);}
}
}
MacOS渲染器:

[assembly: ExportRenderer(typeof(ChatEntry), typeof(ChatEntryRenderer))]
namespace Project.MacOS.Renderers
{
public class ChatEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || e.NewElement == null)
            return;

        var element = (ChatEntry)this.Element;
        var textField = this.Control;
        if (element.Images != null)
        {
            // TODO : Logic GetImageView
        }

        CALayer bottomBorder = new CALayer
        {
            Frame = new CGRect(0.0f, element.HeightRequest - 1, this.Frame.Width, 1.0f),
            BorderWidth = 2.0f,
            BorderColor = element.LineColor.ToCGColor()
        };

        textField.Layer.AddSublayer(bottomBorder);
        textField.Layer.MasksToBounds = true;
    }

    private NSView GetImageView(string imagePath, int height, int width)
    {
        var uiImageView = new NSImageView()
        {
            Frame = new RectangleF(0, 0, width, height)
        };

        uiImageView.Image = NSImage.ImageNamed(imagePath);

        NSView objLeftView = new NSView(new System.Drawing.Rectangle(0, 0, width + 10, height));
        objLeftView.AddSubview(uiImageView);

        return objLeftView;
    }
}
}
[程序集:ExportRenderer(typeof(ChatEntry)、typeof(ChatEntryRenderer))]
命名空间Project.MacOS.Renderers
{
公共类ChatEntryRenderer:EntryRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | e.NewElement==null)
返回;
var element=(ChatEntry)this.element;
var textField=this.Control;
if(element.Images!=null)
{
//TODO:逻辑GetImageView
}
CALayer bottomBorder=新CALayer
{
Frame=新的CGRect(0.0f,element.HeightRequest-1,this.Frame.Width,1.0f),
边框宽度=2.0f,
BorderColor=element.LineColor.ToCGColor()
};
textField.Layer.AddSublayer(底部边框);
textField.Layer.MasksToBounds=true;
}
私有NSView GetImageView(字符串imagePath、整数高度、整数宽度)
{
var uiImageView=new NSImageView()
{
框架=新矩形F(0,0,宽度,高度)
};
uiImageView.Image=NSImage.ImageNamed(imagePath);
NSView objLeftView=新的NSView(新的系统图形矩形(0,0,宽度+10,高度));
objLeftView.AddSubview(uiImageView);
返回objLeftView;
}
}
}

我现在的问题是把这一切结合在一起。我已经创建了一个
GetImageView
,在那里我可以得到一个NSView,但是我如何合并它,使它位于前面的角色(或图像)的右侧,我不确定,需要一些指导。

在所有事情之前,我想说,Xamarin for MacOS仍处于预览阶段,供入门使用,还有一些有待开发的特性,可能会影响你的应用程序,你应该仔细考虑和测试,如果你要释放你的应用程序。您可以找到输入的状态

如果我正确理解了您的问题,您正在尝试创建一个自定义条目,可以将文本和图像放入其中,如下所示:

文本[IMG1]abc[IMG2]

这里有一些东西可能会给你一些想法:


它利用了保存图像和nsmutableAttributeString]来保存条目中的整个字符串。区别在于,它只有一个图像,但条目中有多个图像,因此您可能需要一个字典来将字符串中的占位符与实际图像进行映射。

Hi,似乎
NSTextField
不包含
rightView
的属性,与
UITextField
相同。如果好消息会在这里更新,我会对此进行研究。非常感谢!仍在苦苦挣扎,非常感谢你的回答。我会检查一下,然后开始调整我的代码。可能会在堆栈上创建一个后续问题,但这肯定会将我带入正确的方向。非常感谢