I'm new to WPF and I'm actually migrating a project from Silverlight to WPF 4.0 and my problem is that I can't find the equivalence for IApplicationService and IApplicationLifetimeAware.
The library and namespace System.Windows is well loaded, yet I have the error message;
"The type or namespace name 'IApplicationLifetimeAware' could not be found'.
Any idea what am i missing please.
Thanks
Finally I found that there's no direct migration and all need to be recoded.
but if anyone is implementing IApplicationService and IApplicationLifetimeAware just to access the StatService(), you can simply make the method public, without argument..
public void StartService()
{
.
.
}
and call the method during instanciation in App.xaml.cs
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
InitializeComponent();
OfflineDatabaseService x = new OfflineDatabaseService();
x.StartService();
}
Related
i have the following problem. i have ResourceDictionaries located in different assemblies. if i create UserControls and use Styles and Resourcen from this ResourceDictionaries all works fine on runtime, but at designtime i got errors in vs2010 like - Resource with name "InvertConverter" could not be located.
CoreResource.dll
OtherResource.dll
UserControl.dll (reference the both above)
OtherWpf.dll (reference all above and use the usercontrols)
Now i checked a lot of post and blogs these days related to this problem. one solution was to add the ResourceDictionaries to every UserControl - this would work but create a lot of overhead at runtime. all other solutions i find did not work for me.
i will post what i have done at leat as an answer because it works for me. but i'd like to see other/better soltutions.
here is what i did now.
i simply use a static method to add my resourcedictionaries just at designtime.
public class DesignTimeResourceLoader
{
public static void LoadResources4DesignTime(UserControl ctrl)
{
//do this just in DesignMode
if (Convert.ToBoolean(DesignerProperties.IsInDesignModeProperty.GetMetadata(ctrl).DefaultValue))
{
var uricore = new Uri("/CoreResource;component/ResourceDictionary.xaml", UriKind.Relative);
var core = (ResourceDictionary)Application.LoadComponent(uricore);
ctrl.Resources.MergedDictionaries.Add(core);
var uriother = new Uri("/OtherResource;component/OtherResourceDictionary.xaml", UriKind.Relative);
var other = (ResourceDictionary)Application.LoadComponent(uriother);
ctrl.Resources.MergedDictionaries.Add(other);
//if you have(need more just add here
}
}
}
i create and use this class in my UserControl.dll and for every Usercontrol i call the method in the constructor.
public partial class MyControl : UserControl
{
public MyControl ()
{
DesignTimeResourceLoader.LoadResources4DesignTime(this);
InitializeComponent();
}
}
this works for me atm. bu maybe there are some drawback i did not see now.
I'm moving to using the SimpleIoc for Dependency Injection from using a Static class. There is a Register<TClass>(Func<TClass> factory) where TClass : class method in the SimpleIoc, but I can't find any examples of using it. Here's a link to the source code.
Am I approaching this correctly, or does DI always need to create it at register time? Is this the method I should use to register the class? Can you give me an example on how to do this?
This is the Silverlight code I'm trying to update.
EDIT: moved my approach to an answer
I moved my example from my question to an answer, since no one else has answered it in 22 days. EDIT: this is better.
public partial class App : Application
{
/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
/// </summary>
public App()
{
this.Startup += (s, e) =>
{
// create and register it now
SimpleIoc.Default.Register<IUserToken>(() => { return new UserToken(); });
SimpleIoc.Default.GetInstance<IUserToken>().PopulateUserTokenFromService(() =>
{
// don't do anything until the user token is populated from the server
InitializeComponent();
this.RootVisual = new View();
});
}
}
I'm working on a WinForm application using ninject for dependency injection. My first problem was that the form being instantiated had a parameter (for DI). I added a parameterless constructor thinking this would help. The problem now is that the code inside the constructor with the parameter gets skipped. Here what it looks like:
On my main form:
private void mnuSettings_Click(object sender, System.EventArgs e)
{
frmSettings objForm = new frmSettings();
objForm.Owner=this;
objForm.Show();
}
In the frmSettings form:
private readonly IApplicationPropertiesInterface _applicationProperties;
public frmSettings()
{
InitializeComponent();
}
public frmSettings(IApplicationPropertiesInterface applicationProperties) : this()
{
_applicationProperties = applicationProperties;
}
When I call _applicationProperties.GetExtractFileSaveLocationDirectory() it blows up because the code to set _applicationProperties was never called.
Im wondering if I have structured this incorrectly, and what the best way to achieve this is. My goal is to call the parameterless constructor, but also set _applicationProperties.
Any assistance would be most grateful.
I'm guessing you might be expecting that having Ninject in the building will mena that new will work differently to normal. It doesn't - you need to be doing a kernel.Resolve<Something> for the DI to kick in. Note that most of these pitfalls are covered in detail on the wiki
Can you edit your answer to include details of what you're doing outside of this form please?
In the meantime, here are some previous questions that overlap significantly:-
What is the best practice for WinForms dialogs with ninject?
How to use Ninject in a Windows Forms application?
i have a solution combined with several projects.
this is my App.xaml.cs
public partial class App : Application
{
private SiteManager _siteManager = new SiteManager();
public SiteManager SiteManager
{
get { return _siteManager; }
set { _siteManager = value; }
}
}
during my run i call another project in the same solution
SiteDll.MainWindow siteManagerDialog = new SiteDll.MainWindow();
siteManagerDialog.Show();
but i dont know how to pass all vars in App.xaml.cs to SiteDll.MainWindow siteManagerDialog.
i tried:
SiteDll.MainWindow siteManagerDialog = new SiteDll.MainWindow((App)Application.Current);
siteManagerDialog.Show();
and cast it in SiteDll.MainWindow constructor:
public MainWindow(object me)
{
Application app = ((App)me);
InitializeComponent();
}
but i get casting error...
what is the correct way to do it ?
You are casting YourApplication1.App type to YourApplication2.App which are different types. That's why you get a invalid cast exception.
What you can do is, you can put your common variables in a separate assembly and reference this assembly from both of your applications.
Edit
I would also suggest you to add a 'WPF User Control Library' and then reference it when you are using wpf windows/pages/user controls from other assemblies, to prevent such confusions.
i have a winforms project, and i created a class on assembly A that inherits from System.Windows.Forms.Form to serve as a base class for various forms on my project, the base class is something like:
public partial class DataForm<T> : Form where T : class
{
T currentRecord;
protected T CurrentRecord
{
get
{
return currentRecord;
}
set
{
currentRecord = value;
CurrentRecordChanged();
}
}
}
Now, when i create a form on assembly B that inherits from my DataForm the designer won't load, but if i compile it the app runs fine.
The form looks like:
public partial class SomeTableWindow : DataForm<SomeTable>
{
public SomeTableWindow ()
{
InitializeComponent();
}
}
The error I'm getting is:
The designer could not be shown for this file because none of the classes within it can be designed.
The designer inspected the following classes in the file: CultivosWindow --- The base
class 'TripleH.Erp.Controls.DataForm' could not be loaded. Ensure the assembly has
been referenced and that all projects have been built.
Instances of this error (1)
1. Hide Call Stack
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
Is this a bug on the designer?, Am I doing something wrong? Is there some workaround this?
Thank you for your help
It's a known limitation. Basically you can work around this by declaring another class that inherits from the generic class.
For instance:
class Generic<T> : UserControl
{
}
then
class GenericInt : Generic<int> { }
then use GenericInt instead of Generic. SUcks I know.