Cannot convert lambda expression to System Delegate - delegates

I am creating WPF application. However I got error when using lambda epxressions :
Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(url, UriKind.Relative)));
Please let me know how to solve this. Thanks

Cast your lambda to an Action:
Dispatcher.BeginInvoke((Action)(() => MessageBox.Show(e.Error.Message)));

Related

How to push the data in to one single array in react-hooks

As per code , i want to display the customerNames in a single array but as you see the image below. what is wrong here please can anyone help me thanks in advance
const [names, setNames] = React.useState([]);
socket.on('customers', function (customerNames) =>{
setNames([...names, customerNames])
})
useEffect(() => console.log(names), [names])
setNames([...names, customerNames]) is not recommended because it has no wrapper function, you can use it with a wrapper function like;
setNames(names => [...names, customerNames])

Xamarin Forms / MvvmCross architecture with ICommand.Execute(Object) pattern?

I've started learning some mobile development using Xamarin Forms and MvvmCross.
In its simplest form I have an app that contains a button. When this button is tapped it calls an api via a rest service and returns data via the messaging service.
I'm having issues showing dialogs (ACR) when using the ICommand.Execute(object) pattern. I'm assuming this is because the execute in this pattern is not async and therefore blocks the main ui thread? This is a sample of what I'm doing:
var cmd = Command.Create(CmdType.Login);
cmd.Execute(User);
Please consider the following lines straight from the MvvmCross documentation for async operations.
MyCommand = new MvxCommand(() => MyTaskNotifier = MvxNotifyTask.Create(() => MyMethodAsync(), onException: ex => OnException(ex)));
private async Task MyMethodAsync()
{
await _someService.DoSomethingAsync();
// ...
}
In this case my DoSomething() isn't aysnc - if anything its an async void, or a no no. To get around this I've updated from MvxCommand to MvxAsyncCommand and then have to use a bandaid to get the dialogs to show.
await Task.Delay(300);
or
await Task.Run(() => { command.Execute(User); }).ConfigureAwait(false);
At this point, I'm obviously questioning the use of the command pattern. Am I missing an easy fix or have I chosen an architecture that's not a good fit here? Any guidance is appreciated.
You can use it like this
public ICommand YourCommand => new Command(async () => await YourActionAsync());

Activator with MvvmCross and PCL

I have a very strange problem. Currently, I am porting a Windows Universal App (Win 8.1 + WinPhone 8.1) with Prism to a CrossPlattform Solution with MvvmCross.
First, a big thanks for all the work with MvvmCross - it is amazing.
My problem is now with the System.Activator class. I used this approach for my validation in my Universal App and I tried to port it. Now everything is compiling fine but at runtime an nullreference exception gets thrown. I figured out that it is the Acitivator that is null. When I try to access it in the Immediate windows it says:
error CS0103: The name 'Activiator' does not exist in the current context
The code is executed in every Model and implemented in a base class constructor:
protected ModelBase()
{
foreach (var property in this.GetType().GetRuntimeProperties())
{
var type = typeof(Property<>).MakeGenericType(property.PropertyType);
var prop = (IProperty)Activator.CreateInstance(type);
this.Properties.Add(property.Name, prop);
prop.ValueChanged += (s, e) =>
{
RaisePropertyChanged(property.Name);
Validate();
};
}
}
Any ideas?
Thanks for all the answers. I am feeling a little embarrassed now, because I figured the problem out and it was totally my fault :).
#Anders: You was right, I misspelled the Activator in the Immediate windows and that is why the error was there in the first place - stupid.
The second Problem was the ctor of the generated Object.
public Property()
{
this.Errors.CollectionChanged += (s, e) => RaisePropertyChanged("IsValid");
Errors = new ObservableCollection<string>();
}
Did you find the error? Jap, hooking up to an event on a not created object is a bad idea. Sorry for wasting your time :/ and thanks.

How to bind the 'enabled' property of a monotouch UI element to a viewmodel boolean in Mvvmcross

Writing my login page in IOS Droid using MVVMCross.
this is what I have so far
var bindingSet = this.CreateBindingSet<LoginPageView, LoginPageViewModel>();
bindingSet.Bind(this.UsernameTextField).To(x => x.UserName).TwoWay();
bindingSet.Bind(this.UsernameTextField).For(x=>x.Enabled).To(x => !x.LoggingIn);
bindingSet.Apply();
The binding of 'UserName' successfully binds to the UsernameTextField. However, when the LoginCommand is fired (excluded for brevity) I wan't UI control to be set as 'Enabled = false' whilst the login routine is in progress.
The code above fails at run-time at the x.Enabled binding, with
System.ArgumentException: Property expression must be of the form 'x => x.SomeProperty.SomeOtherProperty'
I must be writing the binding incorrectly, as I DO wish to bind to the 'Enabled' property directly, and not a child prop - but i can't quite see how to do this.
I have looked around some of the samples on mvvmcross, and have looked through a few N+1 videos but I can't seem to find a sample matching enabled, or another child property binding.
Thanks
I couldn't see what was wrong with your code - but I just tried https://github.com/slodge/Enabling and that seemed to work...
So I took another look ... and the problem isn't in the Enabled - instead it is in:
To(x => !x.LoggingIn)
That is not a simple property expression - it's got the ! operator there.
Instead of using ! you can use a ValueConverter like:
public class InverseValueConverter : MvxValueConverter<bool, bool>
{
protected override bool Convert(bool value, ...)
{
return !value;
}
}
Then:
bindingSet.Bind(this.UsernameTextField)
.For(x=>x.Enabled)
.To(x => x.LoggingIn)
.WithConversion(new InverseValueConverter(), null);

MOQ - Mock a Dictionary<string, double> object

I have the following setup for Moq:
... other code to setup bigMoq object ...
var innerMoq = new Mock<IDictionary<string, double>>();
innerMoq.SetupGet(d => d["COMPLEX"]).Returns(6d);
innerMoq.SetupGet(d => d["MEDIUM"]).Returns(8d);
innerMoq.SetupGet(d => d["SIMPLE"]).Returns(10d);
bigMoq.SetupGet(d => d.ComplexityWeights).Returns(x.Object);
When running a test method, I pass in bigMoq as the arguement.
The following works:
bigMoqVar.ComplexityWeights["COMPLEX"] // correctly returns 6
However, this does not:
bigMoqVar.ComplexityWeights.ContainsKey("COMPLEX") // returns false instead of true
What is the recommended way to support the ContainsKey on the innerMoq?
That's because you didn't setup an expectation for ContainsKey. You will need to setup that manually, Moq has no knowledge of the semantics of the interface.
innerMoq.Setup(d => d.ContainsKey("COMPLEX")).Returns(true);
However, if this is just an IDictionary you need, why go through the mocking framework ? Just create a Dictionary<string,double> with the objects you need.

Resources