last day (15 days later) » 

5:55 PM
0
A: C# using MVVM - Difficult Behavior

taquionI have done that by using a custom window service class like following: class WindowsService { private static LoginWindow loginWindow{ get; set; } private static UserWindow UserWindow{ get; set; } public void ShowLoginWindow(LoginViewModel loginViewModel) { LoginWindow...

 
How can I pass the attributes to the UserWindow? And use that attributes in the UserWindowViewModel?
 
When creating the instance of userWindowViewModel in you LoginViewMode, you have to create it with all attributes you need. Then simply call ShowUserWindow(userViewmodel) passing the userWindowViewModel you have just created. If you mean how can you display values of your viewmodel in your view, that is with xaml databinding. Are you familiar to MVVM WPF?
 
I'm learning that. I still didn't understand where I will create the instance of UserWindowViewModel
 
I have edited the answer. Please check it
 
give me a sec. I will try but my LoginView is not a window but a view inside a window.
 
5:55 PM
Doesn´t matter if it is not a window, it is inside a window anyway, so you can call the method always.
 
i.imgur.com/ltAr59P.png I'm having troubles, if you see the LoginViewModel is the child of the MainWindowViewModel. Now I created the Hope that is a test window to this behavior :)
 
I recommend you to look at Emperor Aiman answer.
 
Hello
 
hi
give me a sec
 
Do you saw the error? Do you think that is because in XAML I've already bind the ViewModel?
ok :)
 
5:57 PM
First, look at your ShowLoginWindow
methd
DataContext = "instance" not the class name
in this case is loginViewModel
then, MainWindow.Show is wrong
should be the instance, mainWindow
not the class
same for Hope calling Show
You follow me?
 
I think so give me 30s
in the DataContext
I should put the viewModel?
 
Exactly
That viewModel is the instance of your user in this case, and should contain all properties you want to show in the UI
 
hmm it gave me an error
 
Because you are passing the class name
 
how in the bottom I have userViewModel but I didn't have a ViewModel called user
 
6:03 PM
since you dont have a view LoginView
 
What should I pass?
 
It would be better if you change the parameter name to mainWindowViewmodel instead of loginViewmodel
and you pass this parameter to DataContext
 
so in my example it would be in the first
 
you have to pass loginViewModel, (or mainWindowViewModel if change the name)
 
in noncapital letters?
 
6:04 PM
DataContext = loginViewModel
yeap
And remember to change you AppOnStartUp method
 
Oh. I already understand I'm so dumb -.-'
its the attribute that i receive as a parameter
-.-'
 
exactly
not dumb at all
 
in the App Startup
I should put the LoginViewModel or the MainWindowViewModel?
 
MainWindowViewModel
I wrote loginViewmodel since I thought you have a view like that
 
the view is like that but the parent window is not
like this
 
6:09 PM
do you have an XAML independent file for LoginView?
 
yes :)
it's a user control
 
ohh ok, then you should pass the MainWindowViewModel since is your parent view
you got an error
 
yes
 
because you have to create an instance of windowsService
var ws = new WindowsService()
ws.ShowLoginWindow(this)
 
I was doing that waiting for your answer
and the error is gone :D
I will try now
pray
 
6:18 PM
Lets try
 
nop the error is still there :x
 
show it
please
 
of course
sec
this is my LoginViewModel
with the code you recommend
HopeViewModel: i.imgur.com/KIDbQZw.png
 
you are not creating an instance of windowsservice
you are using class name
var ws = new WindowsService()
ws.ShowLoginWindow(this)
 
in the MainViewModel?
done
lets run
 
6:25 PM
so?
 
Everything went fine
till it change the window
 
=)
 
and crash
 
ooops
lets see
 
it crashed here wService.ShowUserWindow(myUser); (LoginViewModel)
there is some additional information: in portuguese. If you need me to translate for you I will in a heartbeat
 
6:28 PM
thanks, but my mother language is spanish, so they are very similar
remove the call to hide
comment
mainWindow.Hide()
 
awesome. Como estas? :)
 
Muy bien
Hablas español?
Eso haria las cosas mas faciles
 
un poquito
entiendo I escrebo com erros mas acho q nos entendiemos
:D
I think it worked :O
it opens the Hope Window
if the Auth user is there its perfect I will bind a variable to check
 
jejeje
 
(but the previous window didn't close)
 
6:31 PM
thats what I thought
First see if the bind variable works
Hey, I have to eat, I'll be back in an hour. At least we are closer to the end than to the start
 
of course :) I will let the chat open
nice dinner or lunch (I don't know your timezone)
 
6:48 PM
the binding is perfect :D
I will go eat as well. I'll be here ASAP :)
only the Hiding is missing now. I have hope.
 
7:19 PM
I am back
Let me know when you come back
 
7:37 PM
I'm back :)
 
8:18 PM
Sorry, I am little busy, give a couple of minutes
 
np :)
 
8:42 PM
ok
what was the problem???
everything worked fine??
 
yes. I want to close the first window
after the second opens
 
right
please send me an image of your windows service class, startWindowService method and loginviewmodel
 
(I've changed the names because I'm already implementing some new stuff but the logic is working perfectly)
 
ok
now the others please
 
8:50 PM
I see the problem
Lets try the following
the problem is that we are creating two windows service objects
one for main window and the other for userwindow
 
well catched :O
 
in your mainWindowViewmodel or wherever you call StartLoginWindowService and the call to userWindow
make a property in your class
and figure out how to use only one instance for both operations
 
I call the startlogin
in MainWindowViewModel
 
and in which class you do myUser = new GeneralWindowViewModel....?
 
I think I can do it
in the AViewModel
nop
error again.
the AViewModel is an interface that all ViewModels inherit, but didn't work
 
8:59 PM
ok, wait
 
I do that in the LoginViewModel
 
Sorry for the delay
 
no problem :)
 
Look, the idea is that you must use only one instance of WindowService, one way is to share the object with your loginViewModle
 
I tried with the interface
but didn't worked :(
 
9:07 PM
did you remove the older call to "new WindowService" in your loginviemodel??
 
yes I think so
I will check
 
you are suppose not to use wsService variable anymore
but your property
 
nop. Didn't work
LoginViewModel: i.imgur.com/INu8JQO.png
MainWindowViewModel:
internal void StartLoginWindowService()
    {
        ws.ShowLoginWindow(this);
    }
 
of course, because you create a new window service object for each object that implements that interface
lets try another aproach, are you familiar with singleton classes??
try this with your window service class
 
no I didn't know that concept
 
9:16 PM
The idea is that you dont create new instances of windowservice class, the class provides you only one object
check the url
 
    public sealed class WindowService
{
   private static readonly WindowService instance = new WindowService();

   private WindowService(){}

   public static WindowService Instance
   {
      get
      {
         return instance;
      }
   }
}
Like this?
 
exactly
and instead of "var foo = new WindowsService()"
 
inside the get I put the code I have already there?
 
you should call "WindowService.Instance.ShowUserWindow() etc
what code?
 
    public sealed class WindowService
    {
        private static readonly WindowService instance = new WindowService();

        private WindowService() { }

        public static WindowService Instance
        {
            get
            {
                return instance;
            }
        }
        private static MainWindow mainWindow { get; set; }

        private static GeneralWindow generalWindow { get; set; }

        public void ShowLoginWindow(MainWindowViewModel loginViewModel)
Like this?
 
9:19 PM
that should work
with the help of God and all the Avengers
 
still didnt worked :(
 
remember to remove old calls to "new WindowService"
 
yes I've done that
 
what error is this time?
 
same
 
9:21 PM
show your startMainWindow method
 
and finally, your windowservice class entire
again hehhe
 
If this chat let copy paste directly it would be much faster :D I have lightshot but anyway it takes a few seconds
 
do you have teamviewer?
 
yes
ID:
953 352 088
Password: 7635
 
9:29 PM
Can I look at your classes?
 
9:49 PM
mark the question as answered
please
 
yes of course I will
 
thanks
see you
 
done. Upvote and answered :)
see you man
 
=)
 

last day (15 days later) »