Following on from my experimentation with xunit wpf tests I have hit an issue when running multiple tests.
The issue is when I am checking Application.Current.Windows in my assertions.
The Code
To replicate the following code will cause the issue:
TestWindow
<Window x:Class="acme.foonav.Issues.TestWindow"
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:local="clr-namespace:acme.foonav.Issues"
mc:Ignorable="d"
Title="TestWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
Test
public class WpfFactIssues
{
public WpfFactIssues()
{
if (Application.Current == null)
{
new Application();
}
}
[WpfFact]
public void Test1()
{
TestWindow window = new TestWindow();
Assert.Equal(typeof(TestWindow), Application.Current.Windows[0]?.GetType());
}
[WpfFact]
public void Test2()
{
TestWindow window = new TestWindow();
Assert.Equal(typeof(TestWindow), Application.Current.Windows[0]?.GetType());
}
}
So here, Test1 and Test2 are identical. I have removed any other logic not required to demonstrate this scenario to focus on the actual issue - and not why would I want to do this!
The purpose of the scenario was to check whether a window was added into the current application's window collection.
We are using Xunit.StaFact to manage running on the STA Thread.
The Issue
If I execute ALL tests (in Rider) then Test1 will pass, and Test2 will fail on the assertion.
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
However, I can successfully execute Test1 and Test2 individually.
When executing, Test1 will run on say thread id (Thread.CurrentThread.ManagedThreadId) of 20 then Test2 will execute on.
When Test2 executes, then Application.Current is set to what Test1 setup.
What I have tried
Implementing IDisposable and trying calling Application.Current?.Shutdown() in the desperate attempt to make it work.
public void Dispose()
{
if (Application.Current != null)
{
ManualResetEventSlim manualResetEvent = new ManualResetEventSlim(false);
Application.Current.Exit += (sender, args) => { manualResetEvent.Set(); };
Application.Current?.Shutdown();
manualResetEvent.Wait(TimeSpan.FromSeconds(5));
}
}
Here the Exit event is never raised.
This will throw a different exception:
System.InvalidOperationException: Cannot create more than one System.Windows.Application instance in the same AppDomain.
Help!
Is there a way to work with the Application in unit tests when executing lots of methods in the same class?
Update
Currently looking at:
Manage Application.Current while all tests are running in Test Project
I'll post this on here for now, although it solves the issue, it yields a further issue.
If we implement a dispose pattern on the test class, we can force a shutdown of the application. This is somewhat slightly crude.
public void Dispose()
{
if (Application.Current != null)
{
Application.Current.Shutdown(0);
var result = Application.Current.Dispatcher.InvokeAsync(() => { }, DispatcherPriority.ContextIdle).Result;
}
}
However, when the second test runs, the following exception will be thrown:
System.InvalidOperationException: Cannot create more than one System.Windows.Application instance in the same AppDomain.
Don't try this at home!
So lets have some hacky fun with reflection around the internals of Application - to make this work - at least for this scenario:
Let's track our test state around the Application:
public static class FooYou
{
public static bool hasInited = false;
}
This all becomes clear when we new up the Application in our test constructor:
public WpfFactIssues()
{
if (Application.Current == null)
{
new Application()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
if (!FooYou.hasInited)
{
FooYou.hasInited = true;
}
else
{
var appInit = typeof(Application).GetMethod("ApplicationInit", BindingFlags.Static | BindingFlags.NonPublic);
appInit.Invoke(null, null);
}
}
}
One final change in the dispose implementation - pretend the app wasn't created in this app domain otherwise this causes the Cannot create more than one System.Windows.Application instance in the same AppDomain exception.
public void Dispose()
{
if (Application.Current != null)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Shutdown(0);
var result = Application.Current.Dispatcher.InvokeAsync(() => { }, DispatcherPriority.ContextIdle).Result;
var appCreated = typeof(Application).GetField("_appCreatedInThisAppDomain",
BindingFlags.Static |
BindingFlags.NonPublic);
appCreated.SetValue(null, false);
}
}
Here we are forcing the reinit of the Application - as done by the Application's static initializer.
Of course, this is a hideous abomination of a solution. It's dirty. It is of course very susceptible to breaking - never touch a classes privates.
A more workable application of helpers
The above is a little crude, so wrapping up to make the tests work without much effort:
using System.Reflection;
using System.Windows;
using System.Windows.Threading;
namespace acme.foonav.Fixture
{
public static class ApplicationState
{
private static bool hasApplicationPreviouslyInitialized;
public static void CreateNew()
{
Shutdown();
if (Application.Current == null)
{
new Application()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
if (!hasApplicationPreviouslyInitialized)
{
hasApplicationPreviouslyInitialized = true;
}
else
{
var pre = typeof(Application);
var clear = pre.GetMethod("ApplicationInit", BindingFlags.Static | BindingFlags.NonPublic);
clear.Invoke(null, null);
}
}
}
public static void Shutdown()
{
if (Application.Current != null)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Shutdown(0);
var result = Application.Current.Dispatcher.InvokeAsync(() => { }, DispatcherPriority.ContextIdle).Result;
var appCreated = typeof(Application).GetField("_appCreatedInThisAppDomain",
BindingFlags.Static |
BindingFlags.NonPublic);
appCreated.SetValue(null, false);
}
}
}
}
Base class for tests that need to use Application:
using System;
using System.Threading.Tasks;
using System.Windows.Threading;
using acme.foonav.Fixture;
namespace acme.foonav
{
public abstract class ApplicationContextTest : IDisposable
{
protected ApplicationContextTest() => ApplicationState.CreateNew();
public void Dispose() => ApplicationState.Shutdown();
protected async Task AwaitDispatcher() => await Dispatcher.CurrentDispatcher.InvokeAsync(() => { }, DispatcherPriority.ContextIdle);
}
}
A test file:
using System.Windows;
using Xunit;
namespace acme.foonav.Issues
{
public class WpfFactIssues : ApplicationContextTest
{
[WpfFact]
public void Test1()
{
TestWindow window = new TestWindow();
Assert.Equal(typeof(TestWindow), Application.Current.Windows[0]?.GetType());
}
[WpfFact]
public void Test2()
{
TestWindow window = new TestWindow();
Assert.Equal(typeof(TestWindow), Application.Current.Windows[0]?.GetType());
}
}
}
And make sure the tests don't execute in parallel:
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
Related
I have a rather complex project existing of multiple sub projects.
Application
WPF Application
Windows Store App
Windows Phone App
Contracts
Service Contract (WCF for WPF)
Service Controller (WebAPi for windows phone)
Infrastructure (EventAggregator/Prism)
ViewModels (Preferably usable by WPF, Windows Phone and Windows App)
Views (WPF Specific)
As long I concentrate on WPF, I have no problems. In my viewmodel I can call the webservice and fill it with the required data. For Windows Phone/Windows Store App I can't always use WCF. However the viewmodel remains the same. How would I "send" the correct service call to my viewmodel?
public async override Task<object> RetrieveItems()
{
if ((Customer != null))
{
return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
}
else
{
return null;
}
}
This function works fine as long its a wcf service.
Is there some possibility to change this function depending on the view that uses this viewmodel?
Here's the full code of one viewmodel:
namespace ISynergy.Modules.Relations
{
public class Customer_Activities_ViewModel : Customer_Base_ViewModel
{
public Customer_Activities_ViewModel()
: base()
{
}
public override void Add()
{
throw new NotImplementedException();
}
public override Task Delete(object vItem)
{
throw new NotImplementedException();
}
public override void Edit(object vItem)
{
throw new NotImplementedException();
}
public async override Task<object> RetrieveItems()
{
if ((Customer != null))
{
return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
}
else
{
return null;
}
}
}
}
Of course the same applies to the other overridden procedures/functions (Add, Delete and Edit)
Ok, I think I got the answer.
I anyone has a suggestion or comment please be free to provide one.
Maybe this answer helps others with the same problem.
The solution consists of two adjustments.
public async override Task<object> RetrieveItems()
{
if ((Customer != null))
{
return await Task.Run(GetItems_Action);
//return await Task.Run(() => Current.ApplicationService.Schedule_Appointments_GetItems_By_Relation(Customer.Relation_ID));
}
else
{
return null;
}
}
and adding the next lines to allow adjustment from the view.
public Action Add_Action;
public Action<object> Edit_Action;
public Func<object, Task> Delete_Action;
public Func<Task<object>> GetItems_Action;
I have been reading Mark Seeman's book on dependency injection in .NET and I'm struggling to configure composition root in WPF application.
My container will be registered in the application startup method:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var container = new Container();
container.Configure(r =>
{
r.For<IAccountServices>().Use<AccountServicesProxy>();
r.For<MainWindow>().Use<MainWindow>();
});
}
This makes sense as the application startup represents my composition root.
WPF windows in my application are based on view models. View models use constructor injection. E.g. I may compose a view model by injecting implementation of IAccountServices.
When it comes to creating my main window, I can do the following inside of the OnStartup method:
var mainWindow = container.GetInstance<MainWindow>();
mainWindow.Show();
Once I'm inside of the main window, I might want open up another window. So far I've been able to come up with one way of doing this, which is to create a window factory and ask window factory to resolve instance of the window. I'll have to make sure that window factory is available in every view model that might need to open a new window. In my mind this is as bad as passing IoC container around my application (service locator anti-pattern comes to mind).
Does this approach seem right to you? My gut feeling tells me that this is wrong, but I haven't come up with a better way of achieving this (yet).
I think before implement patterns of behavior, such as a Mediator, and the like, need to decide on a generic pattern for easy application structure. For this purpose, namely, for the create independent windows, well suited Abstract factory pattern.
Creation of the windows can be implemented on the side ViewModel using methods such as IDialogService. But I think that this task should be implemented on the side View, because the Window object refers to the View and not to ViewModel. So, you must create MVVM style architecture that it allows create independent windows using design patterns.
I created a project in which an Abstract factory creates a Window on the side of the View using the attached behavior. Abstract factory also implements the Singleton pattern to create a global point of access and to ensure the uniqueness of the newly constructed object. Attached behavior implicitly implements pattern Decorator who is a wrapper for an abstract factory that is used on the side of XAML. To an Abstract factory does not refer to objects which are located in ViewModel is used a Proxy pattern which is a ContentControl with DataTemplate without DataType. Also used Command pattern for independent action between objects. As a result, this project uses the following patterns:
Abstract factory
Singleton
Decorator
Proxy
Command
The project structure looks like this:
In the attached behavior has attached dependency property Name, which is transmitted in the name of the new window. For him registered PropertyChangedEvent, which is a call Make method an abstract factory:
private static void IsFactoryStart(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var window = sender as Window;
if (window == null)
{
return;
}
if (e.NewValue is String && String.IsNullOrEmpty((string)e.NewValue) == false)
{
_typeWindow = (string)e.NewValue;
if (_typeWindow != null)
{
var newWindow = WindowFactory.Instance.Make(_typeWindow);
newWindow.Show();
}
}
}
WindowFactory together with the Singleton pattern looks like this:
public class WindowFactory : IWindowFactory
{
#region WindowFactory Singleton Instance
private static WindowFactory _instance = null;
private static readonly object padlock = new object();
public static WindowFactory Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new WindowFactory();
}
return _instance;
}
}
}
#endregion
public Window Make(string TypeWindow)
{
if (TypeWindow.Equals("WindowOneViewProxy"))
{
var windowOne = new Window();
windowOne.Width = 450;
windowOne.Height = 250;
windowOne.WindowStartupLocation = WindowStartupLocation.CenterScreen;
windowOne.Title = TypeWindow;
windowOne.ContentTemplate = Application.Current.Resources[TypeWindow] as DataTemplate;
return windowOne;
}
else if (TypeWindow.Equals("WindowTwoViewProxy"))
{
var windowTwo = new Window();
windowTwo.Width = 500;
windowTwo.Height = 200;
windowTwo.WindowStartupLocation = WindowStartupLocation.CenterScreen;
windowTwo.Title = TypeWindow;
windowTwo.ContentTemplate = Application.Current.Resources[TypeWindow] as DataTemplate;
return windowTwo;
}
else if (TypeWindow.Equals("WindowThreeViewProxy"))
{
var windowThree = new Window();
windowThree.Width = 400;
windowThree.Height = 140;
windowThree.WindowStartupLocation = WindowStartupLocation.CenterScreen;
windowThree.Title = TypeWindow;
windowThree.ContentTemplate = Application.Current.Resources[TypeWindow] as DataTemplate;
return windowThree;
}
else
throw new Exception("Factory can not create a: {0}" + TypeWindow);
}
}
For the property Window.ContentTemplate set DataTemplate from resources. ContentTemplate is responsible for the visual representation, in order to bind properties from ViewModel, you need to set the object to Content. But in this case, the Abstract factory reference will to ViewModel, and to avoid them and using the proxy pattern as follows:
WindowOneProxyView
<DataTemplate x:Key="WindowOneViewProxy">
<ContentControl ContentTemplate="{StaticResource WindowOneViewRealObject}">
<ViewModels:WindowOneViewModel />
</ContentControl>
</DataTemplate>
WindowOneViewRealObject
<DataTemplate x:Key="WindowOneViewRealObject" DataType="{x:Type ViewModels:WindowOneViewModel}">
<Grid>
<Label Content="{Binding Path=WindowOneModel.TextContent}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Beige" />
<Button Content="One command"
Width="100"
Height="30"
HorizontalAlignment="Center"
Command="{Binding OneCommand}" />
</Grid>
</DataTemplate>
In DataTemplate proxy is not specified DataType, but it is in the real object.
In MainViewModel has commands to simply set the window name, which will give input for attached behavior:
MainModel
public class MainModel : NotificationObject
{
#region TypeName
private string _typeName = null;
public string TypeName
{
get
{
return _typeName;
}
set
{
_typeName = value;
NotifyPropertyChanged("TypeName");
}
}
#endregion
}
MainViewModel
public class MainViewModel
{
#region MainModel
private MainModel _mainModel = null;
public MainModel MainModel
{
get
{
return _mainModel;
}
set
{
_mainModel = value;
}
}
#endregion
#region ShowWindowOneCommand
private ICommand _showWindowOneCommand = null;
public ICommand ShowWindowOneCommand
{
get
{
if (_showWindowOneCommand == null)
{
_showWindowOneCommand = new RelayCommand(param => this.ShowWindowOne(), null);
}
return _showWindowOneCommand;
}
}
private void ShowWindowOne()
{
MainModel.TypeName = "WindowOneViewProxy";
}
#endregion
#region ShowWindowTwoCommand
private ICommand _showWindowTwoCommand = null;
public ICommand ShowWindowTwoCommand
{
get
{
if (_showWindowTwoCommand == null)
{
_showWindowTwoCommand = new RelayCommand(param => this.ShowWindowTwo(), null);
}
return _showWindowTwoCommand;
}
}
private void ShowWindowTwo()
{
MainModel.TypeName = "WindowTwoViewProxy";
}
#endregion
#region ShowWindowThreeCommand
private ICommand _showWindowThreeCommand = null;
public ICommand ShowWindowThreeCommand
{
get
{
if (_showWindowThreeCommand == null)
{
_showWindowThreeCommand = new RelayCommand(param => this.ShowWindowThree(), null);
}
return _showWindowThreeCommand;
}
}
private void ShowWindowThree()
{
MainModel.TypeName = "WindowThreeViewProxy";
}
#endregion
public MainViewModel()
{
MainModel = new MainModel();
}
}
MainWindow looks as:
<Window x:Class="WindowFactoryNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:WindowFactoryNamespace.ViewModels"
xmlns:AttachedBehaviors="clr-namespace:WindowFactoryNamespace.AttachedBehaviors"
AttachedBehaviors:WindowFactoryBehavior.Name="{Binding Path=MainModel.TypeName}"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="300" Width="300">
<Window.DataContext>
<this:MainViewModel />
</Window.DataContext>
<WrapPanel>
<Button Content="WindowOne"
Margin="10"
Command="{Binding ShowWindowOneCommand}" />
<Button Content="WindowTwo"
Margin="10"
Command="{Binding ShowWindowTwoCommand}" />
<Button Content="WindowThree"
Margin="10"
Command="{Binding ShowWindowThreeCommand}" />
</WrapPanel>
</Window>
Test View-ViewModel for the first window looks like this (they practically identical):
WindowOneModel
public class WindowOneModel : NotificationObject
{
#region TextContent
private string _textContent = "Text content for WindowOneView";
public string TextContent
{
get
{
return _textContent;
}
set
{
_textContent = value;
NotifyPropertyChanged("TextContent");
}
}
#endregion
}
WindowOneViewModel
public class WindowOneViewModel
{
#region WindowOneModel
private WindowOneModel _windowOneModel = null;
public WindowOneModel WindowOneModel
{
get
{
return _windowOneModel;
}
set
{
_windowOneModel = value;
}
}
#endregion
#region OneCommand
private ICommand _oneCommand = null;
public ICommand OneCommand
{
get
{
if (_oneCommand == null)
{
_oneCommand = new RelayCommand(param => this.One(), null);
}
return _oneCommand;
}
}
private void One()
{
WindowOneModel.TextContent = "Command One change TextContent";
}
#endregion
public WindowOneViewModel()
{
WindowOneModel = new WindowOneModel();
}
}
This project is available at this link.
Output
MainWindow
WindowOne
WindowTwo
WindowThree
IMHO, there is no need to over complicate the solution for the sake of MVVM purity. You risk the subsequent developers not understanding your elegant solution and break it. In fact there is a good chance of that as "pure" implementations tend to be not that readable because of the complexity.
IMHO, any solution where a problem is permanently solved under an abstraction with minimal code overhead and simplicity in its usage is better than doing considerable overhead every time the solution is used even if "purity" is achieved(it won't serve any purpose). The problem of showing dialog in the application has to be solved once and it should be easy to use it in the future.
Composing view models is perfectly fine, and could make life easier by allowing view models to interact without drama
A dialog service can be created which will act as a wrapper for all your dialog needs in the application. You can inject the Dialog Service and the child view models which needs to be displayed in a window, to your parent view model. When you need to display the window, ask the Dialog service to do it, passing it the view model instance and view name.
Note:code is not complied or tested
public class DialogService : IDialogService
{
IEventAggregator _eventAggregator;
bool _fatalError;
//Provides a wrapper function which will connect your view and view model and open a
//dialog
public Window ShowCustomDialog<TViewModel>(string name, TViewModel viewModel, bool
modal, double left, double top, Action<bool?> OnClose, int width, int height)
{
if (_fatalError == true)
{
return null;
}
Window view = new Window(name);
if (viewModel != null)
{
view.DataContext = viewModel;
}
if (left != -1.0 && top != -1.0)
{
view.WindowStartupLocation = WindowStartupLocation.Manual;
view.Left = left;
view.Top = top;
}
else
{
view.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
if (width != -1 && height != -1)
{
view.Width = width;
view.Height = height;
}
view.Closed += (o, e) =>
{
_eventAggregator.GetEvent<NotifyDialogAction>().Publish(false);
if (OnClose != null)
{
OnClose(e.DialogResult);
}
};
view.Loaded += (o, e) =>
{
_eventAggregator.GetEvent<NotifyDialogAction>().Publish(true);
Window window = o as Window;
if (window != null)
{
double dialogWidth = window.ActualWidth;
double screenWidth =
Application.Current.RootVisual.RenderSize.Width;
double dialogLeft = window.Left;
if (dialogLeft + dialogWidth > screenWidth)
{
window.Left = screenWidth - dialogWidth;
}
double dialogHeight = window.ActualHeight;
double screenHeight =
Application.Current.RootVisual.RenderSize.Height;
double dialogTop = window.Top;
if (dialogTop + dialogHeight > screenHeight)
{
window.Top = screenHeight - dialogHeight;
}
}
};
if (modal)
{
view.ShowDialog();
}
else
{
view.Show();
}
return view;
}
//Add more functions. For example to pop up a message box etc.
}
Usage
public class ComposedVM
{
public ViewModelA objA{get;set;}
public ViewModelB objB{get;set;}
IDialogService dialogService{get;set;}
public ComposedVM(ViewModelA a, ViewModelB b, IDialogService dlg )
{
objA = a;
objB = b;
dialogService = dlg
}
public void OnShowWindowACommand()
{
dialogService .ShowCustomDialog<object>(
DialogNames.ViewA/*view name constant*/, objA, true, -1.0, -1.0,
result =>
{
if (result == true)
{
dialogService.ShowMessageDialog(ApplicationStrings.SuccessFulOperation);
}
});
}
}
An event/message based communication can be used between modules. Using it for related view models in a module is an overkill IMHO.
Pushing container instance through constructor is a bad idea in 99% of cases, because container is a service locator. The main disadvantages of this approach are:
dependency from concrete implementation of container;
unclear API of your classes, which also leads to fragile unit tests.
There are many ways to create window in MVVM fashion:
using Mediators (like IMessenger in MvvmLight, IEventAggregator in Caliburn.Micro);
using special IDialogService;
using attached behaviours;
using Action that inserted via ViewModel constructor;
using Controllers.
I am writing an application with WPF using Prism and the Managed Extensibility Framework. The purpose is to allow developers to create there own 3rd party modules. To remove as much work as possible I have created some base classes in a Common module which already does most of the required work. (eg. drag n drop for views, adds MEF InheritedExportAttribute). One of these controls is called ModuleControl (derives from UserControl) shown below.
I have everything working great if my module has a view class directly derived from UserControl with one child ModuleControl in the XAML (see the examples below). This seems like a lot extra work for users. I'd like to make my view class derive from 'ModuleControl' just using code. If I do this as in the final example below, then RegisterViewWithRegion throws the following exception:
Activation error occured while trying to get instance of type ModuleView, key \"\"
System.Exception {Microsoft.Practices.ServiceLocation.ActivationException}
I realise this happens when the type can't be registered. So my questions are how can I achieve this? What am I doing wrong? Does RegisterViewWithRegion explicitly expect UserControl and nothing derived from it?
Here is an example of my first control located in Common.dll.
namespace Common.Controls
{
[InheritedExport]
public partial class ModuleControl: UserControl
{
private Point _anchor;
public ModuleControl()
{
InitializeComponent();
}
public ObservableCollection<IComponent> ModuleComponents
{
get
{
return (ObservableCollection<IComponent>)GetValue(ModuleComponentsProperty);
}
set
{
SetValue(ModuleComponentsProperty, value);
}
}
private void InitialiseCollection()
{
try
{
ModuleComponents = new ObservableCollection<IComponent>();
var components = ServiceLocator.Current.GetAllInstances(typeof(IComponent));
Assembly controlAssembly = UIHelper.FindAncestor<UserControl>(VisualTreeHelper.GetParent(this)).GetType().Assembly;
foreach (IComponent c in components)
{
if (c.ExportType.Assembly.Equals(controlAssembly))
{
ModuleComponents.Add(new Component(c.ViewType, c.Description));
}
}
}
catch (Exception e)
{
}
}
public string ModuleName
{
get
{
return (string)GetValue(ModuleNameProperty);
}
set
{
SetValue(ModuleNameProperty, value);
}
}
public static readonly DependencyProperty ModuleComponentsProperty =
DependencyProperty.Register("ModuleComponents", typeof(ObservableCollection<IComponent>), typeof(ModuleControl), new UIPropertyMetadata(null));
public static readonly DependencyProperty ModuleNameProperty =
DependencyProperty.Register("ModuleName", typeof(String), typeof(ModuleControl), new UIPropertyMetadata("No Module Name Defined"));
private void DragList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Store the mouse position
_anchor = e.GetPosition(null);
}
private void DragList_PreviewMouseMove(object sender, MouseEventArgs e)
{
// Get the current mouse position
Point mousePos = e.GetPosition(null);
Vector diff = _anchor - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
// Get the dragged ListViewItem
ListView listView = sender as ListView;
ListViewItem listViewItem = UIHelper.FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);
// Initialize the drag & drop operation
if (listViewItem != null)
{
DataObject dragData = new DataObject("moduleFormat", listViewItem.Content);
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);
}
}
}
private void moduleControl_LayoutUpdated(object sender, EventArgs e)
{
if (ModuleComponents == null)
InitialiseCollection();
}
}
Following from the example A Prism 4 Application Checklist I built the following module modifying this to use MEF instead of Unity where appropriate.:
This module is located in RandomNumbers.dll
namespace RandomNumbers
{
[ModuleExport(typeof(RandomNumberModule))]
public class RandomNumberModule: IModule
{
public void Initialize()
{
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
regionManager.RegisterViewWithRegion("MyRegion", typeof(ModuleView));
}
}
}
The ModuleView XAML that works looks like this:
<UserControl x:Name="userControl" x:Class="RandomNumbers.Views.ModuleView"
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:common="clr-namespace:Common.Base;assembly=Common"
xmlns:commonctrls="clr-namespace:Common.Controls;assembly=Common"
mc:Ignorable="d">
<commonctrls:ModuleControl x:Name="moduleControl" ModuleName="Random Number Module" />
</UserControl>
The codebehind for this is:
namespace RandomNumbers.Views
{
[Export]
public partial class ModuleView : UserControl
{
private Point startPoint;
public ModuleView()
{
InitializeComponent();
}
}
}
As already mentioned, all the above code works perfectly. However if I replace the XAML and code behind with this, then I get the exception as described. I have tried leaving out the ExportAttribute but nothing changes.
namespace RandomNumbers.Views
{
[Export(typeof(UserControl))]
public class ModuleView : ModuleControl
{
public ModuleView() : base() { }
}
}
I am Performing a Tutorial I found in Expression Blend 4 for connecting to a SQL Server with WPF. After the final steps in VS12 when I do a build I get the following error.
Error 1 The type or namespace name 'DelegateCommand' could not be found (are you missing a using directive or an assembly reference?)
Error 2 The type or namespace name 'DelegateCommand' could not be found (are you missing a using directive or an assembly reference?)
When I do a Clean I do not get these errors.
My Target is .net 4.5 I also tried 4.0
My code That is erroring looks Like this. I bolded the two erroring lines. this is a file called Class1.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace AWADataSource
{
public class ProductPhotosCollection
{
**private DelegateCommand getDataCommand;
public DelegateCommand GetDataCommand { get { return getDataCommand; } }**
public ProductPhotosCollection()
{
getDataCommand = new DelegateCommand(delegate() { GetData(); });
}
public ObservableCollection<ProductPhoto> ProductPhotos
{ get { return this.productPhotos; } }
private ObservableCollection<ProductPhoto> productPhotos =
new ObservableCollection<ProductPhoto>();
private void GetData()
{
ProductPhotosTableAdapters.ProductPhotoTableAdapter da =
new ProductPhotosTableAdapters.ProductPhotoTableAdapter();
ProductPhotos.ProductPhotoDataTable dt = da.GetData();
productPhotos.Clear();
foreach (ProductPhotos.ProductPhotoRow row in dt)
{
productPhotos.Add(new ProductPhoto(
row.ProductPhotoID,
row.ThumbNailPhoto,
row.LargePhoto,
row.ModifiedDate));
}
}
}
public class ProductPhoto
{
// Public Accessors to the private properties.
public int ID { get { return id; } }
public ImageSource ThumbNailPhoto { get { return thumbNailPhoto; } }
public ImageSource LargePhoto { get { return largePhoto; } }
public DateTime ModifiedDate { get { return modifiedDate; } }
// Constructor.
public ProductPhoto(int id, byte[] thumbNailPhoto, byte[] largePhoto,
DateTime modifiedDate)
{
this.id = id;
this.thumbNailPhoto = ByteArrayToImageSource(thumbNailPhoto);
this.largePhoto = ByteArrayToImageSource(largePhoto);
this.modifiedDate = modifiedDate;
}
// Private properties.
private int id;
private ImageSource thumbNailPhoto;
private ImageSource largePhoto;
private DateTime modifiedDate;
// Supporting method.
private ImageSource ByteArrayToImageSource(byte[] data)
{
BitmapImage image = null;
if (null != data)
{
image = new BitmapImage();
image.BeginInit();
image.StreamSource = new System.IO.MemoryStream(data);
image.EndInit();
}
return image;
}
}
}
and my other file is called DelegateCommand.cs which was pretty much a copy and paist.
namespace AWDataSource
{
using System;
using System.Windows.Input;
///
/// DelegateCommand is a simplified version of ICommand in WPF. You can wrap one of these around any method,
/// and thus bind any command on any WPF object to your method.
///
/// DelegateCommand also supports an IsEnabled property that you can use to turn the command on and off.
///
public sealed class DelegateCommand : ICommand
{
// Remember the method so that it can be called at the right time.
private SimpleEventHandler handler;
// Maintain the enabled state.
private bool isEnabled = true;
// Type signature of the method that DelegateCommand works with - returns void, no arguments.
public delegate void SimpleEventHandler();
// Simple constructor: Pass in the method that needs to be called when the command executes.
public DelegateCommand(SimpleEventHandler handler)
{
this.handler = handler;
}
#region ICommand implementation
// Executing the command is as simple as calling the method.
void ICommand.Execute(object arg)
{
this.handler();
}
// Saying whether the command can be executed.
bool ICommand.CanExecute(object arg)
{
return this.IsEnabled;
}
// This is the event that the command architecture of WPF listens to so it knows when to update
// the UI on command enable/disable.
public event EventHandler CanExecuteChanged;
#endregion
// Public visibility of the isEnabled flag - note that when it is set, the event must be raised
// so that WPF knows to update any UI that uses this command.
public bool IsEnabled
{
get { return this.isEnabled; }
set
{
this.isEnabled = value;
this.OnCanExecuteChanged();
}
}
// Simple event propagation that makes sure that someone is listening to the event before raising it.
private void OnCanExecuteChanged()
{
if (this.CanExecuteChanged != null)
{
this.CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
ProductPhotosCollection is in namespace AWADataSource while DelegateCommand is in AWDataSource.
Probably a typo, but you either need to put them in the same namespace, or use a using to import the AWDataSource namespace into ProductPhotosCollection (or in your case "Class1.cs")
I have a WPF project that is part of the solution, it is "ProjectFilesSelector". A some project, named A. call ProjectFilesSelector as figure below:
!!! UPDATED:
namespace ProjectFilesSelector
{
...
public class ViewModel
{
...
public ICommand cancel
{
get
{
return new WPFExtensions.RelayCommand(_ =>
{
this.window.Visibility = Visibility.Hidden;
this.window.Close();
});
}
}
}
public partial class Window1 : Window, IDisposable
{
public Window1(ProjectTypes.Project pro)
{
InitializeComponent();
var context = new ViewModel(this, new ATChecker.ViewModel.ProjectModel(pro));
this.DataContext = context;
}
...
}
}
namespace ATCheckerView
{
public class ViewerClientExt : INotifyPropertyChanged
{
...
public ICommand CheckPrinciplies
{
get
{
var cmnd =
new RelayCommand(project =>
{
var proj = (ViewModel.ProjectModel)project;
ProjectFilesSelector.ViewModel dc;
using (var a = new ProjectFilesSelector.Window1(proj.project))
{
a.ShowDialog(); // cancel command was called
dc = (ProjectFilesSelector.ViewModel)a.DataContext;
}
....
// some code
// and I can still see the window of Window1. Why?
});
I don t think you want to exit the application. You may just want to close the window.
The cancel button just set IsCancel to true. And for the ok button the best way is to create a event in the viewmodel to get up to the view.