我正在使用Caliburn,ninject 我想创建一个带有背景图像(在网格内部)的飞机驾驶舱
并动态添加(遵循飞机类型)大量用户控件(例如位于不同位置的灯,开关或仪表)
我知道在代码(Grid.Add.Children)之后动态添加Usercontrol
但是Caliburn是否可以使用约定名称并保留在MVVM中?或我必须在后面使用代码..我不知道要解决这个问题 抱歉,我的英语不好,希望您能理解我的意思
感谢您的帮助
文件:BootStrapper.cs
using System;
using System.Collections.Generic;
using System.Windows;
using Caliburn.Micro;
using Ninject;
using WpfApp2.ButtonAndSwitches;
namespace WpfApp2.Bootstrap
{
public class Bootstrapper : BootstrapperBase
{
private IKernel kernel;
public Bootstrapper()
{
Initialize();
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
}
protected override void Configure()
{
kernel = ServiceBootstrapper.Create();
kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
kernel.Bind<MainWindowViewModel>().ToSelf().InSingletonScope();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<MainWindowViewModel>();
}
private void OnSettingsLoaded()
{
}
protected override object GetInstance(Type service, string key)
{
return kernel.Get(service);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return kernel.GetAll(service);
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
kernel.Get<ILog>().Error(e.ExceptionObject as Exception);
}
}
public static class ServiceBootstrapper
{
public static IKernel Create()
{
var kernel = new StandardKernel();
BindCommon(kernel);
return kernel;
}
private static void BindCommon(StandardKernel kernel)
{
kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
}
}
}
InstrumentsViewModel.cs
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfApp2.ButtonAndSwitches;
using WpfApp2.CommonEvents;
using WpfApp2.Extensions;
using IEventAggregator = WpfApp2.Events.IEventAggregator;
namespace WpfApp2
{
public class InstrumentsViewModel : Screen
{
private bool show;
private InstrumentsView instrumentsview;
private MainWindowViewModel mainwindow;
private readonly IEventAggregator eventAggregator;
private readonly int panel;
private readonly IWindowManager windowmanager;
private string _backgroundFile;
public InstrumentsViewModel(IEventAggregator eventAggregator, IWindowManager windowmanager, int panel)
{
this.panel = panel;
this.eventAggregator = eventAggregator;
this.windowmanager = windowmanager;
}
protected override void OnViewReady(object view)
{
if (panel == 0)
{
instrumentsview = view as InstrumentsView;
RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
var backgroundFilestring = @"F:\Ikarus-master\Ikarus\Images\Backgounds\A10CBackGround.png";
BackImage = (backgroundFilestring);
UserControl userControl = (UserControl)Activator.CreateInstance(Type.GetType("WpfApp2.ButtonAndSwitches.SwitchOn_Off_OnView"));
instrumentsview.MainGrid.Children.Add(userControl);
}
}
public string BackImage
{
get => _backgroundFile;
set
{
_backgroundFile = value;
NotifyOfPropertyChange(() => BackImage);
}
}
public void ButtonClose()
{
instrumentsview.Close();
}
public void BackImage_GotFocus()
{
}
public void Window_MouseMove()
{
}
public void Window_MouseDown()
{
}
public void Window_GotFocus()
{
}
}
}
开关示例
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using WpfApp2.CommonEvents;
namespace WpfApp2.ButtonAndSwitches
{
public class SwitchOn_Off_OnViewModel:Screen
{
private string pictureUp = "";
private string pictureMiddle = "";
private string pictureDown = "";
private int state = 1;
public SwitchOn_Off_OnViewModel()
{
SwitchMiddle = @"F:\Ikarus-master\Ikarus\Images\Switches\switch_mid2.png";
}
private string _Switchmiddle;
public string SwitchMiddle
{
get
{
return _Switchmiddle;
}
set
{
_Switchmiddle = value;
NotifyOfPropertyChange(() => SwitchMiddle);
}
}
public void Light_MouseWheel()
{
}
public void UpperRec_MouseDown()
{
if (++state > 2) state = 2;
SetValue(state, true);
}
public void UpperRec_MouseUp()
{
SetValue(1, true);
}
public void LowerRec_MouseDown()
{
if (--state < 0) state = 0;
SetValue(state, true);
}
public void LowerRec_MouseUp()
{
SetValue(1, true);
}
private void SetValue(int _state, bool _event, bool dontReset = false)
{
if (_state == 2)
{
}
if (_state == 1)
{
}
if (_state == 0)
{
}
}
}
}
现在所有视图都已关联
<Window x:Class="WpfApp2.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindowView" Height="158.241" Width="206.594" >
<Grid>
<Button x:Name="ShowOrCloseCockpit" Content="{Binding showorclose}" HorizontalAlignment="Left" Margin="28,29,0,0" VerticalAlignment="Top" Width="112" Height="50"/>
</Grid>
</Window>
<Window x:Class="WpfApp2.InstrumentsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:controls="clr-namespace:WpfApp2.ButtonAndSwitches"
Title="Instruments"
Width="1920"
Height="1080"
AllowsTransparency="True"
Background="Transparent"
cal:Message.Attach="[Event GotFocus] = [Action Window_GotFocus()];[Event Mousedown] = [Action Window_MouseDown()];[Event MouseMove] = [Action Window_MouseMove()]"
Topmost="True"
WindowStyle="None">
<Border Background="Transparent"
BorderBrush="#00FFA500"
BorderThickness="0"
CornerRadius="0">
<Grid x:Name="MainGrid"
Background="#00060606"
Opacity="1.0">
<Image x:Name="BackImage" cal:Message.Attach="[Event GotFocus] = [Action BackImage_GotFocus()]"
Margin="0,0,0,0"
Stretch="Fill" />
<Button x:Name="btCloseRight"
cal:Message.Attach="[Event Click] = [Action ButtonClose()]"
Width="25"
Height="24"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="X"
Foreground="#FF190301"
RenderTransformOrigin="0.895,7.292">
<Button.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#FFF3F3F3" />
<GradientStop Offset="0.5" Color="#FFEBEBEB" />
<GradientStop Offset="0.5" Color="#FFDDDDDD" />
<GradientStop Offset="1" Color="#FFAFAC68" />
</LinearGradientBrush>
</Button.Background>
</Button>
<TextBlock x:Name="MousePosition" HorizontalAlignment="Left" Margin="300,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FFFBF8F8"/>
</Grid>
</Border>
</Window>
和“切换视图”
UserControl x:Class="WpfApp2.ButtonAndSwitches.SwitchOn_Off_OnView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="70" cal:Message.Attach="[Event MouseWheel]=[Light_MouseWheel]">
<Grid Name="Switch" RenderTransformOrigin="0.5,0.5">
<Image x:Name="SwitchUp"
Width="70"
Height="150"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Image x:Name="SwitchMiddle"
Width="70"
Height="150"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Image x:Name="SwitchDown"
Width="70"
Height="150"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Rectangle x:Name="UpperRec" cal:Message.Attach="[Event Mousedown]=[UpperRec_MouseDown];[Event MouseUP]=[UpperRec_MouseUp]"
Width="60"
Height="50"
Margin="5,20,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#00EEDB21"
Stroke="#00000000" />
<Rectangle x:Name="LowerRec" cal:Message.Attach="[Event Mousedown]=[LowerRec_MouseDown];[Event MouseUP]=[LowerRec_MouseUp]"
Width="60"
Height="50"
Margin="5,80,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#00EEDB21"
Stroke="#00000000"/>
<Rectangle x:Name="DesignFrame"
Width="70"
Height="150"
Margin="0,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Fill="#00EEDB21"
Stroke="#FFF38501"
StrokeThickness="4" />
</Grid>
当我单击Mainview中的按钮时,我通过WindowsManager启动instrumentsView
问题是在视图工具旁边动态添加开关(更多)...我如何加载用户控件并与视图绑定(Caliburn?)