using SharedInterfaces; using System; using… · The Chat Archive
Explore using SharedInterfaces; using System; using System.Collections.ObjectModel; using… Save Share Download ▾ Reportusing SharedInterfaces;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using WpfMediaPlayer.ViewModels;
namespace WpfMediaPlayer
{
public class PluginManager
{
public ObservableCollection IPlugins { get; private set; } = new ObservableCollection();
public ObservableCollection IPluginsWithUI { get; private set; } = new ObservableCollection();
public ObservableCollection PluginViewModels { get; private set; } = new ObservableCollection();
/// <summary>
/// 加载指定路径中的所有插件DLL
/// </summary>
/// <param name="pluginsPath">插件文件夹路径</param>
public void LoadPlugins(string pluginsPath)
{
if (!Directory.Exists(pluginsPath))
return;
var dllFiles = Directory.GetFiles(pluginsPath, "*.dll");
foreach (var dll in dllFiles)
{
try
{
Assembly assembly = Assembly.LoadFrom(dll);
var types = assembly.GetTypes();
foreach (var type in types)
{
if (typeof(IPluginWithUI).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
// 插件实现了IPluginWithUI
IPluginWithUI pluginWithUI = (IPluginWithUI)Activator.CreateInstance(type);
pluginWithUI.Initialize();
IPlugins.Add(pluginWithUI);
IPluginsWithUI.Add(pluginWithUI);
PluginViewModels.Add(new PluginViewModel(pluginWithUI));
Console.WriteLine($"Loaded IPluginWithUI: {pluginWithUI.PluginName}");
}
else if (typeof(IPluginBase).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
// 插件仅实现了IPluginBase
IPluginBase pluginBase = (IPluginBase)Activator.CreateInstance(type);
pluginBase.Initialize();
IPlugins.Add(pluginBase);
PluginViewModels.Add(new PluginViewModel(pluginBase));
Console.WriteLine($"Loaded IPluginBase: {pluginBase.PluginName}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load plugin from {dll}: {ex.Message}");
}
}
}
/// <summary>
/// 卸载所有插件
/// </summary>
public void UnloadPlugins()
{
// 卸载IPluginWithUI插件
foreach (var pluginWithUI in IPluginsWithUI)
{
pluginWithUI.GetControl()?.Dispatcher.Invoke(() => pluginWithUI.GetControl().ClearValue(UserControl.ContentProperty));
}
IPluginsWithUI.Clear();
// 卸载IPluginBase插件
IPlugins.Clear();
PluginViewModels.Clear();
}
/// <summary>
/// 添加一个IPluginBase插件
/// </summary>
/// <param name="plugin">插件实例</param>
public void AddIPlugin(IPluginBase plugin)
{
if (plugin != null)
{
plugin.Initialize();
IPlugins.Add(plugin);
PluginViewModels.Add(new PluginViewModel(plugin));
Console.WriteLine($"Loaded IPluginBase: {plugin.PluginName}");
}
}
/// <summary>
/// 添加一个IPluginWithUI插件
/// </summary>
/// <param name="plugin">插件实例</param>
public void AddIPluginWithUI(IPluginWithUI plugin)
{
if (plugin != null)
{
plugin.Initialize();
IPlugins.Add(plugin);
IPluginsWithUI.Add(plugin);
PluginViewModels.Add(new PluginViewModel(plugin));
Console.WriteLine($"Loaded IPluginWithUI: {plugin.PluginName}");
}
}
}
Assistant2024-10-23 00:18 UTC Copy response
了解,我已经查看并记住了您提供的SDK插件共享接口代码。请告诉我接下来需要我做什么。
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 23, 2024
Added to archive Nov 16, 2026
ID 01m7h4202ph5k9ateh7nkzc4zySource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: cd62cbf4e683cb41cd7443a6aea81853
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
}
using System.ComponentModel;
using SharedInterfaces;
namespace WpfMediaPlayer.ViewModels
{
public class PluginParameterViewModel : INotifyPropertyChanged
{
private double _value;
public string Name { get; set; }
public string DisplayName { get; set; }
public double Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
// 更新插件的参数
if (Plugin != null)
{
Plugin.Parameters[Name] = value;
}
}
}
}
public double Minimum { get; set; }
public double Maximum { get; set; }
public IPluginWithUI Plugin { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SharedInterfaces;
using System.Windows.Controls;
namespace WpfMediaPlayer.ViewModels
{
public class PluginViewModel : INotifyPropertyChanged
{
private bool _isEnabled;
public string PluginName => Plugin.PluginName;
public bool IsEnabled
{
get => _isEnabled;
set
{
if (_isEnabled != value)
{
_isEnabled = value;
Plugin.IsEnabled = value;
OnPropertyChanged(nameof(IsEnabled));
}
}
}
public ObservableCollection<PluginParameterViewModel> Parameters { get; set; } = new ObservableCollection<PluginParameterViewModel>();
public IPluginBase Plugin { get; set; }
public UserControl Control
{
get
{
if (Plugin is IPluginWithUI pluginWithUI)
{
return pluginWithUI.GetControl();
}
return null;
}
}
public PluginViewModel(IPluginBase plugin)
{
Plugin = plugin;
IsEnabled = plugin.IsEnabled;
if (plugin is IPluginWithUI pluginWithUI)
{
foreach (var param in pluginWithUI.Parameters)
{
Parameters.Add(new PluginParameterViewModel
{
Name = param.Key,
DisplayName = param.Key, // 可以根据需要更改显示名称
Value = Convert.ToDouble(param.Value),
Minimum = 0, // 根据插件定义设置
Maximum = 2, // 根据插件定义设置
Plugin = pluginWithUI
});
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
// SharedInterfaces/IComponent.cs
using System.Windows.Controls;
namespace SharedInterfaces
{
public interface IComponent
{
///
/// 组件的名称
///
string ComponentName { get; }
/// <summary>
/// 初始化组件
/// </summary>
void Initialize();
/// <summary>
/// 获取组件的 UserControl 界面
/// </summary>
/// <returns>UserControl用于显示</returns>
UserControl GetControl();
/// <summary>
/// 释放组件资源
/// </summary>
void Unload();
}
}
// SharedInterfaces/IPluginWithUI.cs
using System.Collections.Generic;
using System.Windows.Controls;
namespace SharedInterfaces
{
public interface IPluginWithUI : IPluginBase
{
///
/// 获取插件的 UserControl 界面
///
/// UserControl 用于显示
UserControl GetControl();
/// <summary>
/// 获取插件的参数字典
/// </summary>
IDictionary<string, object> Parameters { get; }
}
}
// SharedInterfaces/PluginParameter.cs
using System.ComponentModel;
namespace SharedInterfaces
{
public class PluginParameter : INotifyPropertyChanged
{
private string name;
private double value;
private double min;
private double max;
public string Name
{
get => name;
set { name = value; OnPropertyChanged(nameof(Name)); }
}
public double Value
{
get => value;
set { this.value = value; OnPropertyChanged(nameof(Value)); }
}
public double Min
{
get => min;
set { min = value; OnPropertyChanged(nameof(Min)); }
}
public double Max
{
get => max;
set { max = value; OnPropertyChanged(nameof(Max)); }
}
public PluginParameter(string name, double value, double min, double max)
{
Name = name;
Value = value;
Min = min;
Max = max;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
} 你先看这个SDK PLUG SHARE INTERFACE 记住 但是不回复 接下来给我写个东西
Copy first response
Not yet included in a published release.