Invoking command on TextChange is not updating Text Source Immediately - mvvm-light

I am using MVVM light in my windows phone 8.1 here is code
xaml
<TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="TextChanged">
<Core:InvokeCommandAction Command="{Binding SearchTextChanged}"></Core:InvokeCommandAction>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBox>
here is my VM
private RelayCommand _searchTextChanged;
/// <summary>
/// Gets the SearchTextChanged.
/// </summary>
public RelayCommand SearchTextChanged
{
get
{
return _searchTextChanged
?? (_searchTextChanged = new RelayCommand(
() =>
{
LoadContents(this.SearchText);
}));
}
}
each time text is changed SearchTextChanged command is firing properly but the text in SearchText property is not updated it is one character less. e.g. if text in textbox is A than SearchText contains null. If text in textbox is 'aaa' than text in SearchText is only 'aa' the last character is always missing.
Any Ideas?

OK, so based on the comments, here's the answer - don't use the InvokeCommandAction but the two-way binding.
<TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
In the viewmodel behind this, there's a property called SearchText which has a setter that can call the LoadContents method, something like this...
public string SearchText
{
get { return this.searchText; }
set
{
this.searchText = value;
NotifyPropertyChanged("SearchText");
LoadContents(this.searchText);
}
}
Every time the string in the TextBox changes, the setter is invoked and so is the LoadContents method.

Bumped into a very similar situation but the given workaround wasn't a solution in my case! So discovered another way to update the value.
The solution:
Pass the element as parameter to the command and update the source and then call the method.
public string SearchText
{
get { return this.searchText; }
set
{
this.searchText = value;
NotifyPropertyChanged("SearchText");
}
}
public RelayCommand SearchTextChanged
{
get
{
return _searchTextChanged
?? (_searchTextChanged = new RelayCommand(
() =>
{
someCommandAction();
}));
}
}
private void someCommandAction(object obj)
{
TextBox textBox = (obj as TextBox);
if (textBox != null)
{
var be = textBox.GetBindingExpression(TextBox.TextProperty);
if (be != null)
be.UpdateSource();
}
LoadContents(textBox.Text); //textBox.text or the SearchTextproperty itself
}
XAML part:
<TextBox x:Name="textBoxName" Text="{Binding SearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="TextChanged">
<Core:InvokeCommandAction Command="{Binding SearchTextChanged}"
CommandParameter="{Binding ElementName=textBoxName}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBox>

Related

Xamarin Forms 5.0.0.2515: Relative routing to shell elements is currently not supported

I am receiving the below error :
Relative routing to shell elements is currently not supported. I am running Xamarin forms 5.0.0.2515.
The project navigates to the Items Journal Template View Model where I select an item. The item's name is then passed to the Finished Goods View Model where I will do a search based on the value passed. However I am receiving the above error even though I have already done this on other pages without an error.
I have my routing registered:
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(LocatePage), typeof(LocatePage));
Routing.RegisterRoute(nameof(FinishedGoodsPage), typeof(FinishedGoodsPage));
Routing.RegisterRoute(nameof(ItemJournalTemplatePage), typeof(ItemJournalTemplatePage));
}
ItemJournalTemplateViewModel: I call the Finished Goods page and pass it the item's name...
async void SelectedItemJournalTemplate(ItemJournalTemplate item)
{
if (item == null)
return;
await Shell.Current.GoToAsync($"{nameof(FinishedGoodsPage)}?{nameof(FinishedGoodsViewModel.PassedJournalBatchName)}={item.Name}");
}
and the page receiving the call FinishedGoodsViewModel:
[QueryProperty(nameof(PassedJournalBatchName), nameof(PassedJournalBatchName))]
public class FinishedGoodsViewModel:BaseViewModel
{
private string passedJournalBatchName;
public string PassedJournalBatchName
{
get => passedJournalBatchName;
set
{
passedJournalBatchName = value;
OnPropertyChanged(nameof(PassedJournalBatchName));
}
} .....
}
What makes this odd is that I use this on the FinishedGoodsViewModel to call the LocateViewModel and it works fine:
public async void UpdateLocation(object value)
{
SelectedValue = (FinishedGood)value;
await Shell.Current.GoToAsync($"{nameof(LocatePage)}" +
$"?{nameof(LocateViewModel.ItemNo)}={SelectedValue.Item_No}" +
$"&{nameof(LocateViewModel.BatchName)}={SelectedValue.Journal_Batch_Name}" +
$"&{nameof(LocateViewModel.TemplateName)}={SelectedValue.Journal_Template_Name}" +
$"&{nameof(LocateViewModel.LineNo)}={SelectedValue.Line_No}");
}
LocateViewModel:
[QueryProperty(nameof(ItemNo), nameof(ItemNo))]
[QueryProperty(nameof(BatchName), nameof(BatchName))]
[QueryProperty(nameof(TemplateName), nameof(TemplateName))]
[QueryProperty(nameof(LineNo), nameof(LineNo))]
public class LocateViewModel : BaseViewModel
{
private string batchName;
public string BatchName
{ get => batchName;
set
{
batchName = value;
OnPropertyChanged(nameof(BatchName));
}
}
private string templateName;
public string TemplateName
{
get => templateName;
set
{
templateName = value;
OnPropertyChanged(nameof(TemplateName));
}
}
private string lineNo;
public string LineNo
{
get => lineNo;
set
{
lineNo = value;
OnPropertyChanged(nameof(LineNo));
}
}
private string itemNo;
public string ItemNo
{
get => itemNo;
set
{
itemNo = value;
OnPropertyChanged(nameof(ItemNo));
}
}...
}
Can someone please tell me where I am going wrong here?
It looks like the answer was in the AppShell.xaml file. The original Flyout pointed to the FinsishedGoodsPage but I changed it to ItemJournalTemplatePage, allowing the user to first make a selection first.
<FlyoutItem Title="Finished Goods" Icon="placeholder.png">
<ShellContent Route="FinishedGoodsPage" ContentTemplate="{DataTemplate local:ItemJournalTemplatePage}" />
</FlyoutItem>
I forgot I also needed to change the Route. It probably would not go to the FinishedGoodsPage because it thought it was already on that page.
<FlyoutItem Title="Finished Goods" Icon="placeholder.png">
<ShellContent Route="ItemJournalTemplatePage" ContentTemplate="{DataTemplate local:ItemJournalTemplatePage}" />
</FlyoutItem>

Xamarin forms: How to change the label text value from viewmodel?

I want to change my label text value of the previous page when back button pressed from the new page. I use messagecenter feature from new page and redirect the code flow to the RefreshCustomerDetails() in viewmodel.
I tried like below.
string _fullname = "";
public string FullName
{
protected set
{
if (_fullname != value)
{
_fullname = value;
OnPropertyChanged("FullName");
}
}
get { return _fullname; }
}
public void RefreshCustomerDetails()
{
//FullName = null;
FullName = Application.Current.Properties["customerFullName"].ToString();
}
<Label
x:Name="title_label"
Text="{Binding FullName}"
Font="Bold,18"
TextColor="Black"
Margin="-20,0,0,0"
HorizontalOptions="CenterAndExpand"
VerticalOptions="Center"/>
Taking the Fullname value from Local db and Bind it like above code, but no change in the name when back buttoon press. Try assign null value, that is also not working.
Any correction on my code?
Your code looks good, it should work. Does your ViewModel inherate from INotifyPropertyChanged? This is required for OnPropertyChanged():
public class MyViewModel : INotifyPropertyChanged
{
// stuff ...
}
Updated the code like below, remove RefreshCustomerDetails()
and add Device.BeginInvokeOnMainThread inside of MessagingCenter.
Text="{Binding FullName,Mode=TwoWay}"
MessagingCenter.Subscribe<AddCustomerBySOPage>(this, "Refresh", (sender) =>
{
Device.BeginInvokeOnMainThread(() =>
{
FullName = Application.Current.Properties["customerFullName"].ToString();
});
});

Problems Setting FlipView Items source using Data Virtualization

This is a follow up from my previous question (UWP Problems Setting GridView Items source using x:Bind).
In that case I was using the random access file data source from Mirosoft's Data virtualization sample and the grid was never being populated. The problem there was that I was not raising the property changed event. The grid now works well.
My problem now is that instead of a grid view I am trying to use the data source in a flip view control. The data source does get initialized, the change property notification is raised but nothing is shown in the flip view.
Can a flip view display data from a virtual collection?
Here is my model's code:
public class PhotoFlipViewViewModel : Mvvm.ViewModelBase
{
private FileDataSource _PicturesCollection;
public FileDataSource PicturesCollection
{
get
{
return _PicturesCollection;
}
set
{
if (_PicturesCollection != value)
{
_PicturesCollection = value;
RaisePropertyChanged(() => PicturesCollection);
}
}
}
public PhotoFlipViewViewModel()
{
initdata();
}
async void initdata()
{
StorageLibrary pictures = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
string path = pictures.SaveFolder.Path;
var source = await FileDataSource.GetDataSoure(path);
if (source.Count > 0)
{
PicturesCollection = source;
}
}
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
System.Diagnostics.Debug.WriteLine("PhotoFlipViewViewModel::OnNavigatedToAsync - event fired.");
if ( SessionState.ContainsKey("SelectedPhotoObjectFromGrid"))
{
await Task.CompletedTask;
}
public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
{
if (suspending)
{
}
await Task.CompletedTask;
}
public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = false;
await Task.CompletedTask;
}
}
My FlipView in XAML:
<FlipView x:Name="PhotoOuterFlipView"
BorderBrush="Black"
BorderThickness="1"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
ItemsSource="{x:Bind ViewModel.PicturesCollection, Mode=OneWay}"
ItemTemplate="{StaticResource PictureFlipViewTemplate}">
<FlipView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</FlipView.ItemsPanel>
</FlipView>
Thanks, any advice is appreciated.

Binding with Map API Extensions for Windows Phone 8

I'm trying to use databinding with the map api extension of windows phone toolkit. I'm doing :
<maps:Map x:Name="Map" Center="47.6, -122.3" ZoomLevel="12">
<maptk:MapExtensions.Children>
<maptk:MapItemsControl ItemsSource="{Binding PositionList}">
<maptk:MapItemsControl.ItemTemplate>
<DataTemplate>
<maptk:Pushpin GeoCoordinate="{Binding}" />
</DataTemplate>
</maptk:MapItemsControl.ItemTemplate>
</maptk:MapItemsControl>
</maptk:MapExtensions.Children>
</maps:Map>
with my code behind :
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string name= null)
{
if (object.Equals(variable, valeur)) return false;
variable = valeur;
NotifyPropertyChanged(name);
return true;
}
private IEnumerable<GeoCoordinate> positionList;
public IEnumerable<GeoCoordinate> PositionList
{
get { return positionList; }
set { NotifyPropertyChanged(ref positionList, value); }
}
public MainPage()
{
InitializeComponent();
PositionList = new List<GeoCoordinate>
{
new GeoCoordinate(47.6050338745117, -122.334243774414),
new GeoCoordinate(47.6045697927475, -122.329885661602),
new GeoCoordinate(47.605712890625, -122.330268859863),
new GeoCoordinate(47.6015319824219, -122.335113525391),
new GeoCoordinate(47.6056594848633, -122.334243774414)
};
DataContext = this;
}
}
But I can't see any pushpin on the map :(
What am I doing wrong ?
Note that If I use this in the code-behind file, it's working
MapExtensions.GetChildren(Map).OfType<MapItemsControl>().First().ItemsSource = PositionList;
Thanks in advance for your help,
Best regards
MapItemsControl derives from DependencyObject, not FrameworkElement so the DataContext does not propagate. Long story long... you can't data bind MapItemsControl from XAML unless you have some way to set the Source property of the Binding.
If the FindAncestor mode of RelativeSource worked on the phone, it might be possible to work around this but it apparently does not. This leaves us with either creating the binding in code or (more realistically) setting the ItemsSource in code.

Binding without DependencyProperty

How to bind property in Two Way?
Here is my Code:
public interface IDataOperations
{
string GetData();
string SaveData();
}//close interface
public class Vendor : IDataOperations
{
private string _vendorName = "My Vendor Name";
public string VendorName
{
get { return _vendorName; }
set { _vendorName = value; }
}
}
How can I bind VendorName in my Xaml? I need to bind to Text Box in such a way that if user changes text box value, it should also change the value of that VendorObject too.
Where should I declare VendorObject? Either in xaml.cs file or xaml by using ?
your XAML code should look like
<TextBox Binding="{VendorName}" Name="textBox1" />
and on your Window_Loaded function add this code:
textBox1.DataContext=v;
where v is an instance of the Vendor class.

Resources