|
The fix was instead of using a Using() block of code I changed it to a try/catch:
try ((System.IDisposable)closeWindow.WinObject)
{
((Window)WinObject).Close();
}
catch(Exception e)
{
}
|
|
|
|
|
The Window class[^] does not implement IDisposable . Neither do any of it's base classes. Therefore, you cannot cast a Window to IDisposable .
Why have you declared the field as an object if it's only ever going to store a Window ? Just declare it as Window and avoid the cast.
Your CloseParent method needs to check whether the field has a value; otherwise, you could end up with a NullReferenceException .
You should probably set the field back to null once the window has been closed.
And you could save yourself a lot of trouble by avoiding the static class, and using the Window.GetWindow method[^] instead:
Intake1 window = new Intake1();
window.Show();
Window parentWindow = Window.GetWindow(this);
if (parentWindow != null) parentWindow.Close();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm new to WPF/MVVM and have been trying to come up with a solution for communicating between ViewModels. The best i've been able to come up with is below, it works, but it feels dirty and shameful. Any suggestions would be greatly appreciated.
public delegate void OnMessageReceivedEventHandler(object sender, object message);
public static class Message
{
static List<OnMessageReceivedEventHandler> listeners = new List<OnMessageReceivedEventHandler>();
public static void AddListener(OnMessageReceivedEventHandler onMsgReceived)
{
listeners.Add(onMsgReceived);
}
public static void RemoveListener(OnMessageReceivedEventHandler onMsgReceived)
{
listeners.Remove(onMsgReceived);
}
public static void SendMessage(object sender, object msg)
{
foreach (OnMessageReceivedEventHandler m in listeners)
if (m != null)
m(sender, msg);
}
}
Nothing works better than just improving your product.
|
|
|
|
|
The generally accepted method for communicating between VMs is to use a Mediator or Messenger. If you take a look at MVVM Light, for instance, you can see how to write one and how to use it.
This space for rent
|
|
|
|
|
|
In addition to Pete's answer, you could also use Rx to communicate between viewmodels.
Sacha uses that here: Attached VM Behaviours[^]
|
|
|
|
|
Hello everybody,
I am struggling with the decision how to design my application. Let me describe my problem:
The application has a navigation on the left and the region to display the according view to the right.
It shall load projects and those projects can be of type A, B or C. The navigation is the same for all types, but some of the views differ.
My plan was for all type depending items in the navigation to have a general view which offers a region and the modules A, B and C which all provide a view for that region. During the loading of the project depending on its type the module / library A, B or C is loaded while the other library is unloaded. But I found out it is not supported to unload modules (only complete AppDomains).
I am developing in C# using VS 2010 and Prism 4.0.
Anyone has an idea how to best solve this issue? Is creating and unloading a temporary AppDomain really the best solution?
|
|
|
|
|
I have no idea of what your app is supposed to do, but in my "apps", I often simply toggle the visibility of a component instead of worrying about "loading" and "unloading" UI elements.
|
|
|
|
|
Thank you for your proposal Gerry, that is pretty much what I'll be doing now.
Each library checks if it is applicable for the current type and sets the view's visibility accordingly.
This does not really make me happy as I have to load all assemblies, but it is the best solution I can think of right now.
|
|
|
|
|
Glad that's working for you; sometimes the simplest solution is the best one.
If a better solution comes to mind later (as it often does), you have the option to go back and re-factor if it makes a difference. That's typical of software; when that gnawing feeling stops, you know you are "optimal".
|
|
|
|
|
I have a logon DLL that has a form which needs to be displayed in the main applications main windows navigation Frame. There is no mucking about with app domains but it is a PITA to put it together.
App.xaml - get the arguments passed into the start up exe and pass them to the main window VM.
MainVM has a reference to the main view (I know breaks the MVVM model) and the logon DLL. It gets the URL for the logon view from the DLL code>@"pack://application:,,,/LogonDLLName;component/Views/Logon.xaml and puts it into the frame in the main window.
It subscribes to the LoadCompleted method of the view to get the datacontext of the Logon view. And the PropertyChanged event in the Logon DLL VM.
PropertyChanged gets the content from the logon event and proceeds with the application.
As I said this is a PITA but it does work and we can relatively easily use the same logon DLL for all the applications. I hesitate to put this out there as I'm bloody certain there is a better way to do it.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
What occurs to me is that you can solve this using a combination of this article[^] with some clever work with the AppResources to register your views with "on-demand", replacing view references as necessary.
This space for rent
|
|
|
|
|
I have been posting about my SignalR project and about how I have been unable to connect to the SignalR server component on my server.
Here is a simple Sample SignalR Chat APp.
Run the app, start the server. Then open an additional Client and you'll see that it works fine.
I have then copied the server project's files to my Server, opened a port, disabled the firewall, and attempted to connect.
Depending on the things I try, I get different error messages, all telling me in some way that it could not find the server, or timed out, etc.
At this point I'm really desperate. I MUST get a SignalR server working on my Server. I'd be willing to pay to get help.
If anyone know how to do this, please help.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
What i'm doing wrong?
Basic binding to Combobox ItemsSource.
What I have tried:
C#
public partial class MainWindow : Window
{
ObservableCollection<string> _items = new ObservableCollection<string> { "a1", "a2" };
public ObservableCollection<string> Items
{
get { return _items; }
set { _items = value; }
}
public MainWindow()
{
InitializeComponent();
var aa = a1;
}
}
XAML
<StackPanel>
<ComboBox Name="a1" ItemsSource="{Binding Items}">
</ComboBox>
</StackPanel>
|
|
|
|
|
You haven't set a DataContext. Without that, WPF doesn't know what you are trying to bind to. With the code you have there, after your call to InitializeComponent(); put this in:
DataContext = this; That should do it.
This space for rent
|
|
|
|
|
Yes. It works. Thank you. And for full short answer.
modified 11-Aug-16 0:55am.
|
|
|
|
|
You're welcome. Glad you got it working.
This space for rent
|
|
|
|
|
Not if he is binding to a List of strings!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have searched the internet and can't find an answer to my question. In one of my WPF XMAL pages which is a Views I am using a GridView to load and binding values for each DisplayMemberBinding row. What I need to do is raise a row selected event but the GridView does not contain that event. How do I create a row selected event for the WPF GridView?
|
|
|
|
|
Are you sure you mean a GridView[^]? That can only be used as a view mode for a ListView[^], which does have a SelectionChanged event[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes it is in a ListView. Now I see the SelectionChange event. How would you recommend determining which row was selected?
|
|
|
|
|
|
Richard, Thank you very much.
|
|
|
|
|
Hi,
I'm coding a little application for my company, the idea is very simple:
I would like to copy a directory from a server location to a local folder and I want to show the progress by using a progress bar.
I already integrated the copy of the directory by using this code:
using System;
using System.IO;
class DirectoryCopyExample {
static void Main() {
DirectoryCopy(".", @".\temp", true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) {
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists) {
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName)) {
Directory.CreateDirectory(destDirName);
}
FileInfo[] files = dir.GetFiles();
foreach(FileInfo file in files) {
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
if (copySubDirs) {
foreach(DirectoryInfo subdir in dirs) {
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
For the integration of the progress bar according to the number of files/sizes I can't find anything
Can someone help me with this?
Thank you!
|
|
|
|
|
Member 12674678 wrote: For the integration of the progress bar according to the number of files/sizes I can't find anything Where did you look? Google has lots of suggestions: wpf progress bar - Google Search[^].
|
|
|
|