Avalonia跨平台入门第三十一篇之多选的下拉框

最近再次玩耍Avalonia的时候需要实现一个支持多选的下拉控件效果:

0a4d67cf8793104d1c187ee668f6c08a.gif

1、第一版直接Expander+我的最爱:

<Expander>
  <Expander.Header>
    <TextBlock Text="{Binding SelectedItemsHeader}"/>
  </Expander.Header>
  <ListBox ItemsSource="{Binding LedList}" SelectionMode="Multiple"
       Background="#2D418D" Foreground="White"
       ItemTemplate="{StaticResource LedItemTemplate}"/>
</Expander>

2、关于ListBox中的ItemTemplate:

<DataTemplate x:Key="LedItemTemplate">
  <CheckBox Foreground="White" Margin="20,0,0,0"
        VerticalAlignment="Center" HorizontalAlignment="Left"
        Theme="{StaticResource SimpleCheckBox}"
        IsChecked="{Binding IsSelected}"
        Content="{Binding Name}"/>
</DataTemplate>

3、关于SelectedItemsHeader的逻辑处理:

private void UpdateSelectedItemsHeader()
 {
     var selectedItems = ChannelList.Where(item => item.IsSelected).Select(item => item.Name);
     if (selectedItems != null)
     {
         string showStr = null;
         int count = selectedItems.Count();
         if (count != 0)
         {
             showStr = count > 2? $"{SelectedItemsHeader}...": string.Join(",", selectedItems);
         }
         else
         {
             showStr= "请选择(多选)";
         }
         SelectedItemsHeader = showStr;
     }
 }

4、最后直接封装成一个UserControl:

4a892223da8ff871cb17b0d6fae92bb8.png5、就这么随意玩耍了:

<WrapPanel HorizontalAlignment="Center">
  <Controls:MultipleSelectionComboBox ViewModel="{Binding ComboBoxMainViewModel}"/>
  <Controls:MultipleSelectionComboBox Margin="40,0,20,0" ViewModel="{Binding TwoBoxMainViewModel}"/>
</WrapPanel>

最终简单的效果先这样吧7f7bd86c72304bb33785db8d3f61e4ef.png;以后有时间的话,可以再去摸索一下更复杂的效果7f9f8c0ee00a3a00bcb98f390170ee2a.png;编程不息、Bug不止、无Bug、无生活9b8c83398fe6573e9ba7e951092ccba0.png;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下!

这个问题通常是由于Emgu.CV库在加载C++库时发生的错误导致的。为了解决这个问题,你可以尝试以下步骤: 1.确保你已经按照Emgu.CV的安装指南进行了安装,并且已经添加了所需的引用。 2.在你的项目中添加以下NativeLibrary.SetDllImportResolver代码,该代码会解决安卓项目下无法找到C++库的问题。 ```csharp using System.Runtime.InteropServices; using Emgu.CV; using NativeLibrary = System.Runtime.InteropServices.NativeLibrary; public static class Bootstrapper { public static void Init() { NativeLibrary.SetDllImportResolver(typeof(CvInvoke).Assembly, DllImportResolver); } private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (libraryName == "libgdiplus") { return LoadLibgdiplus(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libgdiplus.so")); } return IntPtr.Zero; } [DllImport("libdl.so")] private static extern IntPtr dlopen(string filename, int flags); [DllImport("libdl.so")] private static extern IntPtr dlerror(); private static IntPtr LoadLibgdiplus(string path) { IntPtr lib = dlopen(path, RTLD_NOW); if (lib == IntPtr.Zero) { var error = Marshal.PtrToStringAnsi(dlerror()); throw new Exception($"Failed to load libgdiplus from '{path}'. {error}"); } return lib; } private const int RTLD_NOW = 2; } ``` 3.在你的Android项目的MainActivity.cs文件中添加以下代码,用于在应用程序启动时初始化Emgu.CV。 ```csharp protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bootstrapper.Init(); // Initialize Emgu.CV // Other initialization code } ``` 通过这些步骤,你应该可以解决System.TypeInitializationException: The type initializer for 'Emgu.CV.CvInvoke' threw an exception问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值