Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:03:21+00:00 2026-05-18T05:03:21+00:00

if try implemnt my first MVVM in WPF app. I have old app based

  • 0

if try implemnt my first MVVM in WPF app. I have old app based on winforms and I want use my logic (class) from this project.

I have Model, it consist with these classes:

public class FriendData
{
//...
}

public class PingData
{
//...
}

public class PokecAccount : INotifyPropertyChanged, IDataErrorInfo
{
//...
}


public class Rp :INotifyPropertyChanged, IDataErrorInfo
{
//...
}


public class JsonUser:INotifyPropertyChanged
{
//...
}

And service class which send HTTP GET and POST request on server, this class implement this interface:

interface IPokecService
{
    bool LogOn(string nick, string password);
    bool LogOff();
    JsonUser CreateJsonUser(string nick);
    void Ping();
    void IbRp();
    bool SendRp(Rp rp);
    Rp LoadRp();
}


public class PokecService:IPokec
{
 public PokecAccount account;
 public PingData pingData;

 //impelent interface IPokec

}

I try use DelegateCommand from WPF Model-View-ViewModel Toolkit 0.1.

With View I have any problem. But my problem is how “wrap” methods from class PokecService in ViewModel.

Example with method LogOn.

First I create View.

StartUpWindow.xaml

    <StackPanel Grid.Row="1" Margin="12,12,12,12">
        <Label Name="lbAzetID" Content="AzetID" Style="{StaticResource lb1}"></Label>
        <TextBox Name="tbAzetID" Style="{StaticResource tb1}">
            <TextBox.Text>
                <Binding Path="AzetId" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged">
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Name="lbRegistration" Content="Registrácia" Style="{StaticResource lb1}">
            <Label.ToolTip>
                <StackPanel>
                    <TextBlock FontWeight="Bold">Registrácia</TextBlock>
                    <TextBlock>Nemáte vlástené AzetID, registrujte sa tu!</TextBlock>
                </StackPanel>
            </Label.ToolTip>
        </Label>
        <Label Name="lbPassword" Content="Heslo" Style="{StaticResource lb1}" ></Label>
        <TextBox Name="tbPassword" Style="{StaticResource tb1}">
            <TextBox.Text>
                <Binding Path="Password" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged">
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Name="lbForgetPassword" Content="Zabudli ste heslo?" Style="{StaticResource lb1}">
            <Label.ToolTip>
                <StackPanel>
                    <TextBlock FontWeight="Bold">Zabudli ste svoje heslo?</TextBlock>
                    <TextBlock>Nechajte si ho zaslať na Váš email.</TextBlock>
                </StackPanel>
            </Label.ToolTip>
        </Label>
    </StackPanel>
    <Button Name="LogOn"
            Command="{Binding LogOnCommand}"
            Content="Prihlásiť" 
            Width="100" 
            Height="25" 
            VerticalAlignment="Center"
            Grid.Row="2" />

It consist only 2 texboxes and one button, I bind button commad on property of ViewModel.

VieModel
StartUpViewModel.cs

In this class I want wrap methods from class PokecService on DelegateCommand, and these bind on UI controls.

public class StartUpViewModel
{

    private string _name = "KecMessanger";
    private string _password = "KecMessanger";
    private PokecService _pokecService;

    public StartUpViewModel()
    {
        _pokecService=new PokecService();
    }

    DelegateCommand _logOnCommand;

    public ICommand LogOnCommand
    {
        get
        {
            if(_logOnCommand==null)
            {
                _logOnCommand=new DelegateCommand(LogOn,CanLogOn);
            }
            return _logOnCommand;
        }
    }

    private void LogOn()
    {
        //In this method I need to call method LogOn from calss PokecService _pokecService.LogOn(_name,_password)          
        //if loging is success I need create another window - view and close this window
        //somehing like this:
        if (_pokecService.LogOn(_name, _password))
        {
            var newViewWindow = new AnotherView();
            //close StartUpView (its typeof window) but I don’t know how
            AnotherView.Show();
        }           
    }

    private bool CanLogOn()
    {
        return true;
    }
}

My questions are:

  1. I can bind UI controls from view on properties in ViewModel, it is no problem.
    It is good way to “wrap” methods from my class PokecService in ViewModel with DelegateCommad?

  2. It is good create new window/View in ViewModel ? How can I close actual View(Window) in ViewModel?

  3. What is the most suitable solution for this problem?

I think I need only wrap my methods with DelegateCommand variable, and create for these variable properties and this properties bind on UI controls but I am absolute beginner in MVVM. I read some articles, but they show only very simple demo.

Thank for any advance. Sorry for my english.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:03:22+00:00Added an answer on May 18, 2026 at 5:03 am
    1. Commands are made to communicate between view and viewmodel in MVVM. To abstract the PokecService away from the ViewModel, I’d advise you to use Dependency Injection (I myself use MEF).
    2. I would supply the view with dependency injection, so the class is easier testable. Create an interface if needed so you could replace the actual implementation of the service class without replacing the type everywhere.
    3. use a DI container.

    Note: I use WAF as MVVM framework together with MEF and it works like a charm.

    Short Example:

    [Export(typeof(IService))]
    public class Service : IService
    {
    }
    
    public interface IService
    {
      void Method();
    }
    
    [Export]
    public class Consumer
    {
      private readonly IService _Service;
    
      [ImportingConstructor]
      public Consumer(IService service)
      {
        ser = service;
      }
    
    
      public void DoStuff()
      {
        //stuff
        _Service.Method();
      }
    }
    

    In a startup method (ie. Application.OnStartup):

    var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()); //insert the assembly defining the mentioned classes above
    var container = new CompositionContainer(catalog);
    container.ComposeParts(this);
    
    Consumer c = container.GetExportedValue<Consumer>();
    c.DoStuff();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.