avalonia 控件-Image(显示图片)

1- resm方式

将图片文件属性设置为 嵌入的资源
在这里插入图片描述
Source=“resm:项目名称.图片文件夹.图片文件.png?assembly=项目名称”

<Image Cursor="Hand" Width="40" Height="40"
Source="resm:TestClient.Resources.title.png?assembly=resm:TestClient"/>

2- avares:// 路径格式

将要显示的图片文件,添加到项目,图片属性选择 AvaloniaResource
在这里插入图片描述

<Image Cursor="Hand" Width="40" Height="40"
Source="avares://TestClient/Assets/title.png"/>

3-svg格式图片显示

GeometryDrawing Brush=“#FFF7FCFF” 表示画图的颜色
GeometryDrawing.Geometry 是具体的图像数据,复用情况下,可放在资源文件中引用

<Image Cursor="Hand" Width="180" Height="180">
            <DrawingImage>
                <DrawingGroup>
                    <GeometryDrawing>
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,0,1024,1024" />
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                    
                    <GeometryDrawing Brush="#FFF7FCFF">                        
                        <GeometryDrawing.Geometry>
                            M512,512 M0,512A512,512,0,1,0,1024,512A512,512,0,1,0,0,512z
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>

                    <GeometryDrawing Brush="#FF48C1FD">
                        <GeometryDrawing.Geometry>
                            M830.577778,756.622222L199.111111,756.622222C195.697778,756.622222,193.422222,754.346666,193.422222,750.933333L193.422222,512C193.422222,509.724444 195.128889,507.448889 196.835556,506.88 196.835556,505.742222 196.835556,504.604444 197.404444,504.035556L292.408889,319.146667C294.115556,315.164445,298.097778,312.888889,302.648889,312.888889L727.04,312.888889C731.591111,312.888889,735.004444,315.164445,737.28,319.146667L832.284444,504.035556C832.853333,505.173334 832.853333,505.742223 832.853333,506.88 835.128889,507.448889 836.266666,509.724444 836.266667,512L836.266667,750.933333C836.266667,754.346666,833.991111,756.622222,830.577778,756.622222z M302.648889,318.577778C300.373333,318.577778,298.666667,319.715556,297.528889,321.422222L202.524444,506.311111 202.524444,510.293333 199.111111,512 199.111111,750.933333 830.577778,750.933333 830.577778,512 826.595556,510.293333 826.595556,506.311111 731.591111,321.422222C730.453333,319.715555,728.746667,318.577778,726.471111,318.577778L302.648889,318.577778z
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingGroup>
            </DrawingImage>
        </Image>

svg文件资源方式引用
1-创建Resource文件,并添加svg文件信息

在这里插入图片描述

<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
    <DrawingGroup x:Key="DrawingGroup">
        <GeometryDrawing Brush="#EDF7FF">
            <GeometryDrawing.Geometry>
              
            </GeometryDrawing.Geometry>
        </GeometryDrawing>

        <GeometryDrawing Brush="#57AFFE">
            <GeometryDrawing.Geometry>
           
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
       
        <GeometryDrawing Brush="#F95F35">
            <GeometryDrawing.Geometry>
             
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingGroup>

    <!--图标-->
    <DrawingGroup x:Key="DelDrawingGroup">
        <GeometryDrawing Brush="#606266">
            <GeometryDrawing.Geometry>
                M85.333 490.667l853.333 0 0 42.667-853.333 0 0-42.667z
            </GeometryDrawing.Geometry>
        </GeometryDrawing>        
    </DrawingGroup>
    
</ResourceDictionary>

2-要使用图片页面所在的项目App.axaml文件添加引用

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>             
                <ResourceInclude Source='avares://TestResource/SvgIcons.axaml'/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>  

3-页面显示添加引用

<Button Click="Btn_Click" Classes="IconStyle" IsVisible="{Binding ElementName=rdoBtn,Path=IsChecked}" >
       <Image Stretch="Uniform" Width="20" Height="20" >
            <DrawingImage Drawing="{StaticResource DelDrawingGroup}"/>
       </Image>
</Button>

3.4-图像单一情况下也可使用

直接显示:

 <Image Stretch="Uniform" Width="22" Height="22">
    <DrawingImage>
        <GeometryDrawing Brush="#606266" Geometry="M85.333 490.667l853.333 0 0 42.667-853.333 0 0-42.667z"/>
    </DrawingImage>
</Image>

 <Image Stretch="Uniform" Width="22" Height="22">
    <DrawingImage>
        <GeometryDrawing Brush="#606266">
         M85.333 490.667l853.333 0 0 42.667-853.333 0 0-42.667z
        </GeometryDrawing>
    </DrawingImage>
</Image>

使用资源:

<Image Stretch="Uniform" Width="22" Height="22">
   <DrawingImage>
       <GeometryDrawing Brush="#606266" Geometry="{StaticResource DelGeometry}"/>
   </DrawingImage>
</Image>

图像资源:

<ResourceDictionary xmlns="https://github.com/avaloniaui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Geometry x:Key="Geometry">      
      </Geometry>

      <Geometry x:Key="DelGeometry">
            M85.333 490.667l853.333 0 0 42.667-853.333 0 0-42.667z
      </Geometry>

4-使用代码创建Image

var img = new Image
    {
        Source = new DrawingImage
        {
            Drawing = new ImageDrawing
            {
                ImageSource = new Bitmap(BitmapPath),
                Rect = new Rect(0, 0, 200, 200),
            }
    }

5-后台代码生成xaml页面控件

1-添加NuGet包引用,Avalonia.Markup.Xaml.Loader

在这里插入图片描述

2-使用

 string imageXaml = @"<Image xmlns='https://github.com/avaloniaui' Name='img' Stretch='Uniform' Width='22' Height='22'>
<DrawingImage Drawing = '{StaticResource DrawingGroup}'/> 
</Image>";               
          
if (!string.IsNullOrEmpty(imageXaml))
 {
    var image = (Image)AvaloniaRuntimeXamlLoader.Load(imageXaml);
    btn.Content = image;
 }

6-代码设置Source

6.1-Image.Source=byte[]

        /// <summary>
        /// byte[]转IBitmap
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static IBitmap ByteToBitmap(byte[] bytes)
        {
            IBitmap bmpImg;
            try
            {
                if (bytes == null || bytes.Length == 0)
                {
                    bmpImg = null;
                }
                else
                {
                    bmpImg = new Bitmap(new MemoryStream(bytes));
                }
            }
            catch
            {
                bmpImg = null;
            }
            return bmpImg;
        }

//使用
Image image = new Image();
image.Source = ByteToBitmap(imageBytes);

6.2-Image.Source=图片路径

   public static IBitmap UriToBitmap(string uri)
   {
      IBitmap bitmap = null;
      var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
      bitmap = new Bitmap(assets.Open(new Uri(uri)));         
      if (bitmap == null)
      {
         bitmap = new Bitmap(uri);
      }
      return bitmap;
   }

//使用
Image image = new Image();
image.Source = UriToBitmap();

6.3-Image.Source=图片路径

Image image = new Image
{
  Source = new DrawingImage
  {
   Drawing = new ImageDrawing
   {
     ImageSource = new Bitmap(BitmapPath),
     Rect = new Rect(0, 0, 200, 200),
   }
 }
}

6.4-Image.Source=图片文件数据

  DrawingGroup drawingGroup = new DrawingGroup();
  var geometryDrawing = new GeometryDrawing
  {
    Pen = new Pen(SolidColorBrush.Parse("#909399")),
    Geometry = (Geometry)
  };
  drawingGroup.Children.Add(geometryDrawing);
  (image.Source as DrawingImage).Drawing = drawingGroup;

 //或者
  DrawingImage drawingImage = new DrawingImage(drawingGroup);
  image.Source = drawingImage;
Avalonia是一种用于构建跨平台用户界面的开源框架。导航控件Avalonia中的一种重要控件,用于在不同页面之间进行导航操作。 Avalonia的导航控件允许我们在应用程序中创建多个页面,并且能够在这些页面之间进行平滑的切换。导航控件提供了一种结构化的方式来组织和管理应用程序的不同界面。通过导航控件,我们可以通过简单的命令或代码逻辑来实现页面的导航和跳转。 导航控件通常由两个主要组件组成:导航器和页面容器。导航器负责维护当前页面的状态,并提供导航操作的方法和事件。页面容器用于显示和管理不同的页面。当我们进行页面导航时,导航器会负责加载、显示和销毁页面。 对于使用Avalonia导航控件的应用程序,我们可以在页面间使用导航器提供的方法来切换页面,这样可以实现应用程序的整体流程控制。例如,我们可以使用导航控件在登录页面和主页面之间进行导航,或者在主页面的不同子页面之间进行导航。 此外,Avalonia导航控件还可以与其他控件一起使用,以实现更复杂的用户界面。例如,我们可以将导航控件和菜单控件结合使用,以创建具有导航功能的应用程序菜单。我们也可以将导航控件和数据绑定一起使用,以实现基于数据驱动的页面导航。 总而言之,Avalonia导航控件是一种实用的工具,可以帮助我们在Avalonia应用程序中有效地管理和导航页面,提供良好的用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值