0

After looking at https://www.youtube.com/watch?v=fOookEq5od0 I tried to implement my own version. It is, however, not working as fluently as I hoped. Something weird is happening in my xaml when I try to acess the ViewModel. The project is rather big to paste it all in here, but I'll try to show the relevant code and explain my best.

So I've created a ViewModel class in a folder called "ViewModel". In the same folder, I also have a UICommands class. In the ViewModel I have a function called "SaveTask" that I want to bind to a button in my MainWindow. Those two classes are in the namespace TaskClient.ViewModels. Everything else in the WPF is in the TaskClient namespace.

ViewModel.cs :

namespace TaskClient.ViewModels
{

    public class ViewModel
    {
        TaskServiceReference.TaskServiceClient _taskServiceClient = new       
                   TaskServiceClient()
        public UICommands UICommands { get; set; }

       public ViewModel ()
       {
            this.UICommands = new UICommands(this);

       }
       public void SaveTask()
       {
            Debug.WriteLine("hello");
       }

}

UICommands.cs :

namespace TaskClient.ViewModels
{
    public class UICommands : ICommand
    {


        public ViewModel viewModel { get; set; }

        public UICommands(ViewModel viewModel)
        {
            this.viewModel = viewModel;

        }
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            this.viewModel.SaveTask();
        }
    }
}

Mainwindow.xaml :

<Window x:Class="TaskClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TaskClient"
    xmlns:vm="clr-namespace:TaskClient.ViewModels"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <vm:ViewModel x:Key="viewModel"/>
</Window.Resources>

EDIT : App.config :

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ITaskService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:64162/Service1.svc"   

                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ITaskService"      

                contract="TaskServiceReference.ITaskService"
            name="BasicHttpBinding_ITaskService" />
    </client>
</system.serviceModel>

EDIT : Web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking"  
type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule,    
Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below  
to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
           <!-- To receive exception details in faults for debugging  
purposes, set the value below to true.  Set to false before deployment to 
avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"  
multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" 
type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, 
Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value  
 below to true.
        Set to false before deployment to avoid disclosing web app folder  
information.
      -->
    <directoryBrowse enabled="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>

</configuration>

So I'm trying to reference the viewModel class in the xaml, but it won't let me. I get the following "error" (yellow text underline) under "" : Could not find default endpoint element that references contract "TaskServiceReference.ITaskService" in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoin element matching this contract could be found in the client element.

But I'm having a hard time relating to the explanation. Yes, I have a service reference in my client to a "TaskServiceReference.ITaskservice", but how is that relevant in this situation? My ViewModel class is a local reference, what does the service reference have anything to do this? And also, I have a configuration to the TaskServiceReference.ITaskService as you can see in App.config file. I also supplied my Web.config file now

EDIT 2 : If I remove the declaration of the _taskServiceClient in the ViewModel constructor, the error goes away. But it doesn't exactly fix the problem, cus I need the serviceclient..

user1784297
  • 103
  • 12
  • it is complaining in the app.config file. – sexta13 Feb 18 '16 at 22:44
  • 2
    This a WCF question, not WPF. The serviceModel bindings are not correctly set up. It has nothing to do with the command in WPF. – Troels Larsen Feb 18 '16 at 23:20
  • The ViewModel code cannot compile – Emond Feb 19 '16 at 08:26
  • But they are, I tink. I have already binded a grid in the xaml to a function in the ViewModel called HotelTasks which returns a couple of tasks. That works perfectly. But when I add this line "" it gives me the error. – user1784297 Feb 19 '16 at 12:52
  • Can you maybe please suggest a solution to this? I've been struggling for the last 2 days to solve this. – user1784297 Feb 19 '16 at 12:56
  • Your view model code above won't even compile. You can't declare fields in a namespace. –  Feb 19 '16 at 17:44
  • No, that's just a paste error from my side. It's really in the ViewModel class. So any other suggestions? – user1784297 Feb 19 '16 at 18:50

0 Answers0