How can I bind a command to a button in code in MvvMCross (Xamarin.iOS) with specifying a command parameter?
// command definition
public MvxCommand SaveDealerDataCommand
{
get { return new MvxCommand<bool>(DoSaveDealerDataAction); }
}
public void DoSaveDealerDataAction(bool show)
{
//...
}
// binding
bindingset.Bind(saveButton).To(vm => vm.SaveDealerDataCommand);
Where can I specify the parameter (true/false) that will be passed to the command?
Android and iOS buttons don't have CommandParameter properties in the same way that Windows ones do.
However, MvvmCross did recently introduce a way to introduce CommandParameter bindings via Value Converters - see http://slodge.blogspot.co.uk/2013/06/commandparameter-binding.html
This binding should work as:
bindingset
.Bind(saveButton)
.To(vm => vm.SaveDealerDataCommand)
.WithConversion("CommandParameter", true);
or:
bindingset
.Bind(saveButton)
.To(vm => vm.SaveDealerDataCommand)
.WithConversion(new MvxCommandParameterValueConverter(), true);
Note that this CommandParameter binding isn't completely in the 3.0.8.1 package which is the stable nuget release, so to make this work you may need to either:
Add this manual value converter registration in your Setup.cs
protected override void FillValueConverters(IMvxValueConverterRegistry registry)
{
base.FillValueConverters(registry);
registry.AddOrOverwrite(
"CommandParameter",
new Cirrious.MvvmCross.Binding.MvxCommandParameterValueConverter()
);
}
Or use one of the beta nuget packages uploaded since 3.0.8.1 (set nuget to include prereleases to see these packages).
Or build the source yourself
To achieve your dynamic command parameter using the text in one of your UITextField controls, you could bind the text in that UITextField to a string property on you ViewModel and the code that runs in your button's bound command would be able to access the value via that property when it executes.
In your ViewController, something like:
UITextField textField = new UTextField();
textField.Frame = new RectangleF(0,0,120,30);
Add(textField);
UIButton button = new UIButton();
button.Frame = new RectangleF(70,40,50,30);
button.SetTitle("Click Me");
Add(button);
var bindingSet = this.CreateBindingSet<MyView, MyViewModel>();
bindingSet.Bind(textField).To(vm => vm.StringProperty);
bindingSet.Bind(button).To(vm => vm.ClickCommand);
bindingSet.Apply();
Then, in your ViewModel:
private string _stringProperty = string.Empty;
public string StringProperty
{
get { return _stringProperty; }
set
{
_stringProperty = value;
RaisePropertyChanged(() => StringProperty);
}
}
public ICommand ClickCommand
{
get
{
return new MvxCommand(HandleClick);
}
}
public void HandleClick()
{
//Code that accesses StringProperty (which contains the UITextField's value)
}
To pass a dynamic command parameters to the command in the view model you could create a new class e.g. like this DynamicCommandParameterValueConverter:
/// <summary>
/// This class is inspired by MvvmCross MvxCommandParameterValueConverter,
/// but because we need dynamic command parameters, we need to implement our own combined with a CustomMvxWrappingCommand.
/// </summary>
/// <typeparam name="T">The type of the 'selected item' for the command</typeparam>
/// <typeparam name="TResult">The returned result that is used as input for the command.</typeparam>
public class DynamicCommandParameterValueConverter<T, TResult> : MvxValueConverter<ICommand, ICommand>
{
private readonly Func<T, TResult> commandParameterExpression;
public DynamicCommandParameterValueConverter(Func<T, TResult> commandParameterExpression)
{
this.commandParameterExpression = commandParameterExpression;
}
protected override ICommand Convert(ICommand value, Type targetType, object parameter, CultureInfo culture)
{
return new CustomMvxWrappingCommand<T, TResult>(value, commandParameterExpression);
}
}
The CustomMvxWrappingCommand takes a Func as argument, and is later invoked and passed into the commands CanExecute/Execute method. Here is a snippet of how part of that class could look like:
public void Execute(object parameter)
{
if (wrapped == null)
{
return;
}
if (parameter != null)
{
Mvx.Warning("Non-null parameter overridden in MvxWrappingCommand");
}
wrapped.Execute(commandParameterOverride((T)parameter));
}
You could modify the MvxWrappingCommand class from Mvx to implement the above example.
The use of it all:
set.Bind(myControl).For(control => control.ItemClick).To(vm => vm.MyCommand).WithConversion(
new DynamicCommandParameterValueConverter<MyModel, string>((MyModel item) =>
{
// Do custom logic before parsing the item..
}));
Related
I am struggling to understand how to apply a custom ValueConverter to all the properties of all the models before every insert and update.
According to this resource it can be done:
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations
"Value converters allow property values to be converted when reading from or **writing ** to the database"
I was able to accomplish this, but only when reading from the database, overriding ConfigureConventions in the Context:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<string>().HaveConversion<ToUpperConverter>();
}
Converter
public class ToUpperConverter : ValueConverter<string, string>
{
public ToUpperConverter() : base(v => v, v => v.Trim().ToUpper())
{
}
}
Tried also to override the method SaveChangesAsync in the Context, this works but obviously I don't want to go manually for every property and for every model considering they can change at every time.
Using reflection here can be an option, but I am not really a big fan of it.
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
foreach (var entry in ChangeTracker.Entries<Player>())
{
if (entry.State == EntityState.Modified || entry.State == EntityState.Added)
{
entry.Entity.Name = entry.Entity.Name.ToUpper();
entry.Entity.MiddleName = entry.Entity.MiddleName?.ToUpper();
entry.Entity.Surname = entry.Entity.Surname.ToUpper();
}
}
return await base.SaveChangesAsync(cancellationToken);
}
Solved, after spending on this an entire day..
The first argument of the converter is the conversion happening to the database, the second argument is the conversion happening from the database.
public class ToUpperConverter : ValueConverter<string, string>
{
public ToUpperConverter() : base(
// writing to the database
v => v.Trim().ToUpper(),
// reading from the database
v => v.Trim().ToUpper())
{
}
}
Source: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.propertiesconfigurationbuilder.haveconversion?view=efcore-6.0
"The type to convert to and from or a type that inherits from ValueConverter."
So I'm trying to use Autofac Automocking in ASP.NET MVC 5, but for some reason I can't get it to work.
Here's the test so far:
using (var mock = AutoMock.GetLoose())
{
const string mainUserID = "MainUserID";
const string otherUserID = "OtherUserID";
ApplicationUser user = new ApplicationUser()
{
Id = mainUserID,
UserName = "TestUser"
};
var dataProvider = mock.Mock<IDataProtectionProvider>();
dataProvider.DefaultValue = DefaultValue.Mock;
var userManagerMock = mock.Mock<ApplicationUserManager>();
}
The test fails when mocking the ApplicationUserManager. The error is this:
Result StackTrace:
at Autofac.Extras.Moq.AutoMock.Mock[T](Parameter[] parameters)
at AwenterWeb_NUnit.AccountControllerTest.<Deactivate_User>d__0.MoveNext() in C:\Users\Fabis\Documents\Docs\KvalifikÄcijas darbs 2015\AwenterWeb\AwenterWeb-NUnit\AccountControllerTest.cs:line 51
at NUnit.Framework.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)
at NUnit.Core.NUnitAsyncTestMethod.RunTestMethod()
Result Message: System.InvalidCastException : Unable to cast object of type 'AwenterWeb.ApplicationUserManager' to type 'Moq.IMocked`1[AwenterWeb.ApplicationUserManager]'.
The same thing happens when trying to automock the ApplicationDbContext and it has a very simple constructor, so there shouldn't even be any issues with it.
I'm new to Mocking - what should I do in this scenario?
Edit: Also kind of an unrelated question, maybe you guys know - I've noticed that when creating a Moq for a DbSet using a list created previously in the test, I have to do this:
var dbSetMock = new Mock<IDbSet<DbEntity>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
It seems really unintuitive. Is there a way to just tell the mock to take the list? So something like:
dbSetMock.Setup(m => m).Returns(data);
Or any other way to create a DbSet Moq from an existing list quickly without having to write those 4 extra lines?
If you look at ligne 73 of MoqRegistrationHandler.cs you can see that only interface is moqable using Autofac.Extras.Moq
var typedService = service as TypedService;
if (typedService == null ||
!typedService.ServiceType.IsInterface ||
typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
typedService.ServiceType.IsArray ||
typeof(IStartable).IsAssignableFrom(typedService.ServiceType))
return Enumerable.Empty<IComponentRegistration>();
var rb = RegistrationBuilder.ForDelegate((c, p) => CreateMock(c, typedService))
.As(service)
.InstancePerLifetimeScope();
You can change the code but it may be quite difficult to make it works with non parameter less dependency.
Can your dependencies be changed to use an interface instead of a concrete class ? if it is not possible and/or if it doesn't make sense, you can use the MockRepository to create your non parameter-less component and then inject it on the AutoMock class.
class Program
{
static void Main(string[] args)
{
using (var mock = AutoMock.GetLoose())
{
/// configure your non interface component with constructor parameters
/// if foo need more complex parameters you can get them
/// using mock.Mock<T>().Object
var fooMock = mock.MockRepository.Create<Foo>((String)null);
fooMock.SetupGet(f => f.Value).Returns("test");
// insert your instance into the container
mock.Provide<Foo>(fooMock.Object);
var bar = mock.Create<Bar>();
Console.WriteLine(bar.GetValue());
}
}
}
public class Foo
{
public Foo(String value)
{
this._value = value;
}
private readonly String _value;
public virtual String Value
{
get
{
return this._value;
}
}
}
public interface IBar
{
String GetValue();
}
public class Bar : IBar
{
public Bar(Foo foo)
{
this._foo = foo;
}
private readonly Foo _foo;
public String GetValue()
{
return this._foo.Value;
}
}
It is not a perfect solution but without big refactoring of the Autofac.Extras.Moq project I can't see any simpler way to do it.
I am relatively new to Mvvm and MvvmCross. I've encountered a problem in iOS;
This is my delayed binding for my MvxTableViewCell-subclass, which I've put in AwakeFromNib:
this.DelayBind(() =>
{
var bindingSet = this.CreateBindingSet<MyTableViewCell, MyViewModel>();
bindingSet.Bind(myLabel).To(vm => vm.Name).WithConversion(debugvalueconverter, base.DataContext);
});
My debugvalueconverter looks like this:
public class DebugValueConverter : IMvxValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
System.Diagnostics.Debug.WriteLine("Converting debug: " + value);
if (parameter != null)
{
var vm = (MyViewModel)parameter;
System.Diagnostics.Debug.WriteLine("Converting debug - name from vm: " + vm.Name);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new NotImplementedException();
}
}
This generates some odd outputs when I'm starting to recycle cells when dragging in the tableview:
Converting debug: [Name from the cell that's appearing]
Converting debug - name from vm: [Name from an old cell which got recycled]
I.e. the DataContext doesn't seem to be set correctly in this case, BUT the name-property is set correctly. Why is this happening?
My problem is that I want to use a converter that will do some "advanced" calculations according to what's in a few properties of my ViewModel (I.e. my base.DataContext).
Is this a bug in MvvmCross or am I missing something? Why is not the DataContext set correctly here?
Thanks!
I don't think you can send in the view model that way to the cell. You need to do the binding in your view and bind your cell to the collection of objects in your view model from the view. Typically I do something like this:
public partial class MainView : MvxViewController
{
/// <summary>
/// Views the did load.
/// </summary>
/// <summary>
/// Called when the View is first loaded
/// </summary>
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new MvxSimpleTableViewSource(MainTableView, MainInspectionCell.Key, MainInspectionCell.Key);
MainTableView.Source = source;
var set = this.CreateBindingSet<MainView, MainViewModel>();
set.Bind(source).To(vm => vm.Inspections);
set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.ItemSelectedCommand);
set.Apply();
MainTableView.ReloadData();
}
And then for the cell I typically do the binding in the constructor of the cell like so:
public partial class MainInspectionCell : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName("MainInspectionCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString("MainInspectionCell");
public MainInspectionCell(IntPtr handle) : base (handle)
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<MainInspectionCell, Inspection>();
set.Bind(InspectionCell).For(v => v.BackgroundColor).To(vm => vm.StatusColor).WithConversion("NativeColor");
set.Apply();
});
}
public static MainInspectionCell Create()
{
return (MainInspectionCell)Nib.Instantiate(null, null)[0];
}
}
}
Hope this helps! I think the main issue is that you are trying to send the view model directly into the cell which is not there. If that is something you are needing to accomplish you will need to create some kind of wrapper around your collection object which sends in the view model you need to access.
the generated code from EF for a property of an entity looks like this:
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.DateTime DateCreated
{
get
{
return _DateCreated;
}
set
{
OnDateCreatedChanging(value);
ReportPropertyChanging("DateCreated");
_DateCreated = StructuralObject.SetValidValue(value);
ReportPropertyChanged("DateCreated");
OnDateCreatedChanged();
}
}
private global::System.DateTime _DateCreated;
partial void OnDateCreatedChanging(global::System.DateTime value);
partial void OnDateCreatedChanged();
This code doesn't check if the value has actually changed (in the setter). Therefore the PropertyChanged event is raised even if you set a value that is equal to the current value. But in this case nothing would have changed, so I wouldn't want this event...
For EntityKey properties they do check this:
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Guid Id
{
get
{
return _Id;
}
set
{
if (_Id != value)
{
OnIdChanging(value);
ReportPropertyChanging("Id");
_Id = StructuralObject.SetValidValue(value);
ReportPropertyChanged("Id");
OnIdChanged();
}
}
}
private global::System.Guid _Id;
partial void OnIdChanging(global::System.Guid value);
partial void OnIdChanged();
I would expect this behavior from all properties.
Am I missing a setting in the model designer, or is there another solution?
Thanx!
It is point of T4 templates to allow you modifications you need. It is absolutely wrong approach to say:
But I would rather not use a custom template in my project!
It is like throwing all advantages of T4 templates away and going back to hardcoded custom tools for code generating.
I did, as I knew it was possible and Ladislav also stated, include the T4 template file into the project and made the following changes to the "Write PrimitiveType Properties." part of the template:
if (!Object.Equals(<#=code.FieldName(primitiveProperty)#>, value))
{
<#=ChangingMethodName(primitiveProperty)#>(value);
ReportPropertyChanging("<#=primitiveProperty.Name#>");
<#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
ReportPropertyChanged("<#=primitiveProperty.Name#>");
<#=ChangedMethodName(primitiveProperty)#>();
}
Hope that will be helpfull to others.
I use the FluentValidation framework to add validation and annotations to my models in an MVC project.
I need to add data annotations to the class level of a model. Namely, the model needs to have the DisplayColumn attribute added. But, since I use FluentValidation (and have the application's ModelMetadataProvider set to use FluentValidation), even if I put the DisplayColumn attribute on the model class, it isn't used. However, I can't find a way to add that annotation by using FluentValidation.
Does anyone have any idea how I can get that to work?
Thanks
I wouldn't actually recommend using the FluentValidationModelMetadataProvider - this was only really ever an experimental addition (which very well may be removed from the next release), and it doesn't support any of the class-level DataAnnotations (such as DisplayColumn). I would suggest that you use FluentValidation only for validation, but stick with attributes for metadata.
That being said, if you really want to get this working then you could implement by using a custom no-op validator that's used only for metadata:
public static class MetadataExt {
public static IRuleBuilderOptions<T, TProperty> DisplayColumn<T, TProperty>(this IRuleBuilder<T, TProperty> rule) {
var ruleBuilder = (FluentValidation.Internal.RuleBuilder<T, TProperty>)rule;
ruleBuilder.Rule.AddValidator(new DisplayColumnWrapper(ruleBuilder.Rule.PropertyName));
return ruleBuilder;
}
public class DisplayColumnWrapper : NoopPropertyValidator, IAttributeMetadataValidator {
private string name;
public DisplayColumnWrapper(string name) {
this.name = name;
}
public override IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) {
return Enumerable.Empty<ValidationFailure>();
}
public Attribute ToAttribute() {
return new DisplayColumnAttribute(name);
}
}
}
... Which you could then use like this:
public class Validator : AbstractValidator<SomeModel> {
public Validator() {
RuleFor(x => x.DisplayColumnProperty)
.DisplayColumn();
}
}
You'd then need to create a custom ModelMetadataProvider that knows how to process this:
public class ExtendedFVModelMetadataProvider : FluentValidationModelMetadataProvider {
IValidatorFactory _validatorFactory;
public ExtendedFVModelMetadataProvider(IValidatorFactory validatorFactory)
: base(validatorFactory) {
this._validatorFactory = validatorFactory;
}
public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType) {
var validator = _validatorFactory.GetValidator(modelType);
if (validator == null) {
return base.GetMetadataForType(modelAccessor, modelType);
}
// Only look for the DisplayColumnWrapper
// There is a mismatch as MVC expects this to be defined at class-level, but FV defines everything at the property level.
var displayColumns = from memberWithValidator in validator.CreateDescriptor().GetMembersWithValidators()
from propertyValidator in memberWithValidator
let wrapper = propertyValidator as MetadataExt.DisplayColumnWrapper
where wrapper != null
select wrapper.ToAttribute();
var displayColumn = displayColumns.FirstOrDefault();
// we found a displaycolumn, so pass it over to MVC to build the metadata.
if (displayColumn != null) {
return CreateMetadata(new[] { displayColumn }, null /* containerType */, modelAccessor, modelType, null /* propertyName */);
}
return base.GetMetadataForType(modelAccessor, modelType);
}
}
The provider overrides the GetMetadataForModel method and looks for any properties that use DisplayColumn. This could probably be extended if you want to support any other custom metadata extensions too. You could then use this provider in place of the metadata provider that comes with FluentValidation.
However, I still wouldn't recommend this approach...the library is designed for performing validation, not for generating UI metadata.