马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
IRegionManager 和IContainerExtension
IRegionManager 是 Prism 框架中用于管理 UI 区域(Regions)的焦点接口,它实现了模块化应用中视图(Views)的动态加载、导航和生命周期管理。
IContainerExtension 是依赖注入(DI)容器的抽象接口,而 Resolve<T>() 方法用于从容器中解析指定范例的实例
-
- public class u1: UserControl
- {
- IRegionManager _regionManager;
- IContainerExtension _container;
- public u1(IContainerExtension container, IRegionManager regionManager)
- {
- _regionManager = regionManager;
- _container = container;
-
- //从容器中解析ListView类型的实例。如果ListView已注册为单例,则返回单例实例;否则返回新实例
- ListView ListView11 =_container.Resolve<ListView>();
- //获取中心显示区域
- IRegion region= _regionManager.Regions["ContentRegion"];
- //为中心显示区域添加视图(ListView11),并为视图分配一个名称“ListView1”
- region.Add(ListView11 , "ListView1");
- //将指定视图(ListView11)设置为区域(region)中的活动视图
- region.Activate(ListView11);
- }
- }
复制代码 u1的xaml中有:
<ContentControl Grid.Column="1" prism:RegionManager.RegionName="ContentRegion" />
......
<dx XTabItem Header="名称">
<local istView/>//将 ListView 视图嵌入到 DXTabItem 中,作为选项卡页的内容。
</dx XTabItem>
此中ListView是自定义的另一个用户控件。
在 Prism 框架中,联合第三方控件库(如 DevExpress 的 DXTabItem)时,可以通过 XAML 直接定义视图(如 ListView)并将其嵌入到选项卡控件中。
region.Activate(view) 方法用于将指定视图(view)设置为区域(IRegion)中的运动视图。
单选框的动态绑定
<StackPanel Margin="20">
<TextBlock Text="组合单选框" FontWeight="Bold"/>
<DockPanel x:Name="GroupRadioButton">
<StackPanel DockPanel.Dock="Left">
<ItemsControl ItemsSource="{Binding RadioButtons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding SecurityId}"
IsChecked="{Binding IsSelected, Mode=TwoWay}"
GroupName="RadioButtons"
Command="{Binding DataContext.RadioCheckCommand,
RelativeSource={RelativeSource AncestorType=Window}}"
CommandParameter="{Binding}">
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
<StackPanel DockPanel.Dock="Right"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{Binding SelectedRadioButton.SecurityId, StringFormat='结果:{0}'}" />
</StackPanel>
</DockPanel>
</StackPanel>
- using Prism.Commands;
- using Prism.Mvvm;
- using StrategyClient.Models;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Data;
- namespace StrategyClient.ViewModels
- {
- class SellStrategyViewModel : BindableBase
- {
- /// <summary>
- /// 当前选择的单选框
- /// </summary>
- private ConfigAccount _selectedRadioButton;
- /// <summary>
- /// 当前选择的单选框
- /// </summary>
- public ConfigAccount SelectedRadioButton
- {
- get => _selectedRadioButton;
- set => SetProperty(ref _selectedRadioButton, value);
- }
- /// <summary>
- /// 需要显示的一组单选框的信息链表
- /// </summary>
- public ObservableCollection<ConfigAccount> RadioButtons { get; } = new ObservableCollection<ConfigAccount>();
- /// <summary>
- /// 绑定命令触发(单选框选择改变时)
- /// </summary>
- public DelegateCommand<ConfigAccount> RadioCheckCommand { get; }
-
- public SellStrategyViewModel()
- {
- // 初始化单选框选项
- RadioButtons.Add(new ConfigAccount { SecurityId = "选项1", IsSelected = false });
- RadioButtons.Add(new ConfigAccount { SecurityId = "选项2", IsSelected = false });
- RadioButtons.Add(new ConfigAccount { SecurityId = "选项3", IsSelected = false });
- // 设置默认选中项
- if (RadioButtons.Count > 0)
- {
- RadioButtons[0].IsSelected = true;
- SelectedRadioButton = RadioButtons[0];
- }
- // 注册命令
- RadioCheckCommand = new DelegateCommand<ConfigAccount>(OnRadioChecked);
- }
- private void OnRadioChecked(ConfigAccount item)
- {
- // 更新选中项
- foreach (var radioButton in RadioButtons)
- {
- radioButton.IsSelected = radioButton == item;
- }
- SelectedRadioButton = item;
- }
-
- }
- }
复制代码 查抄UI绑定路径
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
......
<CheckBox IsChecked="{Binding IsSelectedV, Mode=TwoWay, diagnostics resentationTraceSources.TraceLevel=High}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
假如直接绑定到 IsSelectedV 属性不起作用,可以尝试使用 CellValue 绑定:
<CheckBox IsChecked="{Binding RowData.Row.IsSelectedV, Mode=TwoWay}"/> 在这种情况下,RowData.Row 通常是指当前行的数据对象。
实现弹窗功能:
//App中注册对话框
containerRegistry.RegisterDialog<View, ViewModel>();
// 显示弹窗
void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback);
// 显示模态弹窗(阻塞式)
void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback);
主视图的ViewModel:
public ObservableCollection<string> Items
{
get;
set;
} = new ObservableCollection<string>();
/// <summary>
/// 按钮按下弹窗
/// </summary>
private void Button_Click()
{
//转达参数
var parameters = new DialogParameters
{
{ "DataList", Items }
};
//View:视图名,parameters:要转达的参数
_dialogService.ShowDialog("View", parameters, (IDialogResult result) =>
{//弹窗关闭后回调函数
// 从结果中获取数据链表
if (result.Parameters.TryGetValue<ObservableCollection<string>>("DataList", out var dataList))
{
Items = dataList;
}
});
}
// 定义弹窗事件
public class ShowDialogEvent : PubSubEvent<DialogParameters> { }
// 发布弹窗请求
var parameters = new DialogParameters { { "message", "生存成功!" } };
_eventAggregator.GetEvent<ShowDialogEvent>().Publish(parameters);
// 弹窗服务订阅事件并显示弹窗
_eventAggregator.GetEvent<ShowDialogEvent>()
.Subscribe(ShowDialog, ThreadOption.UIThread);
private void ShowDialog(DialogParameters parameters)
{
_dialogService.ShowDialog("MessageDialog", parameters);
}
View视图的ViewModel
class ViewModel : BindableBase, IDialogAware
{
public ObservableCollection<string> Items
{
get => _items;
set => SetProperty(ref _items, value);
}
/// <summary>
/// 对话框事件,转达对话框的结果
/// </summary>
public event Action<IDialogResult> RequestClose;
/// <summary>
/// 关闭对话框时转达参数
/// </summary>
public event Action<IDialogParameters> RequestClosed;
// 对话框标题
public string Title => "弹窗标题";
/// <summary>
/// 允许关闭对话框
/// </summary>
/// <returns></returns>
public bool CanCloseDialog()
{
return true;
}
/// <summary>
/// 关闭对话框时
/// </summary>
public void OnDialogClosed()
{
var resultParameters = new DialogParameters
{
{ "DataList", Items }
};
// 触发请求关闭事件
RequestClosed?.Invoke(resultParameters);
}
/// <summary>
/// 打开对话框时
/// </summary>
/// <param name="parameters"></param>
public void OnDialogOpened(IDialogParameters parameters)
{
if (parameters.TryGetValue<ObservableCollection<SellStrategyModel>>("DataList", out var initialName))
{
Items = initialName;
}
}
}
单选框:
<DockPanel x:Name="GroupRadioButton">
<ItemsControl ItemsSource="{Binding RadioButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Id}"
IsChecked="{Binding IsSelected, Mode=TwoWay}"
GroupName="RadioButtons"
Command="{Binding DataContext.RadioCheckCommand,
RelativeSource={RelativeSource AncestorType=Window}}"
CommandParameter="{Binding}">
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTe
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|