Well then , I got something like this in my manipange.xaml , my application bar declaration, and my button declaration/definition
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar x:Name="xxxx" IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton
x:Name="appBarRegisterButton"
IconUri="/Images/next.png"
Text="Login"
Click="appBarRegisterButton_Click_1" IsEnabled="True"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
when i try to change some property from some other model, fe.
this is my registartion Model.
public RegisterViewModel()
{
RegisterTitle = Resources.AppResources.RegisterTitle;
Messenger.Default.Register<RegisterButtonPressed>(this, msg => AttemptToRegister(msg.reg));
}
private void AttemptToRegister(Register reg)
{
reg.appBarRegisterButton.IsEnabled = true;
}
It does not change,better my register button is a null object, anyone got a point how to solve this? ;/
You have to access the button like this:
var applicationBarIconButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
if (applicationBarIconButton != null)
{
//do stuff
}
The index depending on where it is located in the appbar.
Related
How to access a property of an element of a page in xamarin forms from Android and IOS projects?
For example:
Button1.IsVisible = false;
but since the Android project.
you could access properties of page elements in the code behind file, but that is generally not a good or clean strategy.
A simple sample would be Page.xaml:
<Button x:Name="Button1" Content="Sample String"/>
And then in your Page.xaml.cs
Button1.IsVisible = false;
The better way is that you might want to check out the DataBinding mechanism:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/
As a small example you would have a BindingContext/ViewModel that implements INotifyPropertyChanged. With that you could go like this:
private bool _isVisible;
public bool IsVisible
{
get => this._isVisible;
set
{
if(this._isVisible != value){
this.IsVisible;
}
}
And then in your Page.xaml:
<Button IsVisibile="{Binding IsVisible} Content="Sample String"/>
You can set the BindingContext inside your Page.xaml.cs:
this.BindingContext = new ViewModel(); //If the properties are in a separate class (preferred)
this.BindingContext = this; //If the properties are in the page class
Thanks, I solved it with this:
MessagingCenter.Subscribe<object>(this, "btButton1Visible", (sender) =>
{
Button1.IsVisible = false;
});
I have a Xamarin.Form MainPage:ContentPage with a ToolbarItems Bar on top. The toolbar items are bound to my ViewModel like so:
<ToolbarItem Text="Sync" Command="{Binding ReloadCommand}" >
</ToolbarItem>
The availability of the Item depends on some logic:
private bool canReloadExecute(object arg)
{
bool result = (!IsReloading && (App.GetPersistentSetting("DeviceID") != "") && (App.GetPersistentSetting("BuildingID") != ""));
return result;
}
There is a separate dialog controlling the DeviceID and BuildingID on a different settings page. Once any of those ids is entered it is persistently stored away
App.SetPersistentSetting("DeviceID",value);
Problem is, that the menu items don't change their appearance once my code uses popAsync() to return to the Main page. I need to restart my app to see the changes. According to the debugger, canReloadExecute isn't called. Why this?
What I tried to work around this issue is to force a refresh in the MainPage's OnAppearing method like this:
public void RefreshToolbarItems()
{
TestApp.ViewModels.MainViewModel mvm = (TestApp.ViewModels.MainViewModel)BindingContext;
mvm.RefreshToolbarItems();
}
... and in the ViewModel:
public void RefreshToolbarItems()
{
OnPropertyChanged("BuildingScanCommand");
OnPropertyChanged("ReloadCommand");
}
but this code runs through but changes nothing, while the Debugger shows that the routine is indeed firing the events, they seem to go nowhere.
Any ideas how I can get my menu going?
Edit 1: "Show command initalization"
I am not shre what specifically you mean, but here is the whole code dealing with the command:
private ICommand _reloadCommand;
public ICommand ReloadCommand => _reloadCommand ?? (_reloadCommand = new Command(ExecuteReloadCommand, canReloadExecute));
private bool _isReloading = false;
public bool IsReloading
{
get => _isReloading;
set
{
_isReloading = value;
((Command)_reloadCommand).ChangeCanExecute();
OnPropertyChanged(nameof(ReloadCommand));
}
}
private bool canReloadExecute(object arg)
{
bool result = (!IsReloading && (App.GetPersistentSetting("DeviceID") != "") && (App.GetPersistentSetting("BuildingID") != ""));
return result;
}
private async void ExecuteReloadCommand(object obj)
{
IsReloading = true;
// Some code ...
IsReloading = false;
}
The goal is to disable the command, if either the command handler is already running, or if the configuration of DeviceID and/or BuildingId hasn't been done yet.
The enable/disable does almost work, if I set the DeviceId and BuildingId, and restart the app, the command is properly enabled or disabled. It doesn't work, however, if I set the Ids in a sub-page and return to the main page.
meanwhile I came to the conclusion, that firing onPropertyChange obviously doesn't make the command check its canReloadExecute. So the question is, how do I trigger this?
I finally solved the issue myself, this code works nicely for me:
public void RefreshToolbarItems()
{
((Command)BuildingScanCommand).ChangeCanExecute();
((Command)ReloadCommand).ChangeCanExecute();
}
I'm implementing a DynamicItemStart button inside a Menu Controller. I'm loading the dynamic items for this button when Visual Studio starts. Everything is loaded correctly so the initialize method is called an I see all the new items in this Dynamic button. After the package is completely loaded I want to add more items to this Dynamic button, but since the package is already loaded the initialize method is not called again and I cannot see the new items in this Dynamic button. I only see the ones that were loaded when VS started.
Is there any way that I can force the update of this Dynamic button so it shows the new items?. I want to be able to update the VS UI after I added more items but outside the Initialize method.
The implementation I did is very similar to the one showed on this msdn example:
http://msdn.microsoft.com/en-us/library/bb166492.aspx
Does anyone know if an Update of the UI can be done by demand?
Any hints are greatly appreciated.
I finally got this working. The main thing is the implementation of a derived class of OleMenuCommand that implements a new constructor with a Predicate. This predicate is used to check if a new command is a match within the DynamicItemStart button.
public class DynamicItemMenuCommand : OleMenuCommand
{
private Predicate<int> matches;
public DynamicItemMenuCommand(CommandID rootId, Predicate<int> matches, EventHandler invokeHandler, EventHandler beforeQueryStatusHandler)
: base(invokeHandler, null, beforeQueryStatusHandler, rootId)
{
if (matches == null)
{
throw new ArgumentNullException("Matches predicate cannot be null.");
}
this.matches = matches;
}
public override bool DynamicItemMatch(int cmdId)
{
if (this.matches(cmdId))
{
this.MatchedCommandId = cmdId;
return true;
}
this.MatchedCommandId = 0;
return false;
}
}
The above class should be used when adding the commands on execution time. Here's the code that creates the commands
public class ListMenu
{
private int _baselistID = (int)PkgCmdIDList.cmdidMRUList;
private List<IVsDataExplorerConnection> _connectionsList;
public ListMenu(ref OleMenuCommandService mcs)
{
InitMRUMenu(ref mcs);
}
internal void InitMRUMenu(ref OleMenuCommandService mcs)
{
if (mcs != null)
{
//_baselistID has the guid value of the DynamicStartItem
CommandID dynamicItemRootId = new CommandID(GuidList.guidIDEToolbarCmdSet, _baselistID);
DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId, isValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem);
mcs.AddCommand(dynamicMenuCommand);
}
}
private bool IsValidDynamicItem(int commandId)
{
return ((commandId - _baselistID) < connectionsCount); // here is the place to put the criteria to add a new command to the dynamic button
}
private void OnInvokedDynamicItem(object sender, EventArgs args)
{
DynamicItemMenuCommand invokedCommand = (DynamicItemMenuCommand)sender;
if (null != invokedCommand)
{
.....
}
}
private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args)
{
DynamicItemMenuCommand matchedCommand = (DynamicItemMenuCommand)sender;
bool isRootItem = (matchedCommand.MatchedCommandId == 0);
matchedCommand.Enabled = true;
matchedCommand.Visible = true;
int indexForDisplay = (isRootItem ? 0 : (matchedCommand.MatchedCommandId - _baselistID));
matchedCommand.Text = "Text for the command";
matchedCommand.MatchedCommandId = 0;
}
}
I had to review a lot of documentation since it was not very clear how the commands can be added on execution time. So I hope this save some time whoever has to implement anything similar.
The missing piece for me was figuring out how to control the addition of new items.
It took me some time to figure out that the matches predicate (the IsValidDynamicItem method in the sample) controls how many items get added - as long as it returns true, the OnBeforeQueryStatusDynamicItem gets invoked and can set the details (Enabled/Visible/Checked/Text etc.) of the match to be added to the menu.
I have tried to bind the contents of a textbox to a property I created inside the control, but without success. I have found a way to do it otherwise, but it is convoluted, and I'd prefer something simpler. Anyway, this is the final code:
public partial class DateListEditor : UserControl, INotifyPropertyChanged {
private int _newMonth;
public int newMonth {
get { return _newMonth; }
set {
if(value < 1 || value > 12)
throw new Exception("Invalid month");
_newMonth = value;
NotifyPropertyChanged("newMonth");
}
}
public DateListEditor() {
InitializeComponent();
DataContext = this;
newMonth = DateTime.Now.Month;
}
// ...
Then in the XAML:
<TextBox x:Name="uiMonth" Text="{Binding newMonth, Mode=TwoWay, ValidatesOnExceptions=True}"/>
This thing works. It will pre-fill the textbox with the current month, and validate it when focus is lost: great.
But how can I avoid the XAML line, and do everything from code? I can't seem to be able to work this out. I have tried this code, but nothing happens:
InitializeComponent();
Binding b = new Binding("Text") {
Source = newMonth,
ValidatesOnExceptions = true,
Mode = BindingMode.TwoWay,
};
uiMonth.SetBinding(TextBox.TextProperty, b);
DataContext = this;
How can I do it without setting the binding in the XAML?
Try changing this line and see if it helps
//oldway
Binding b = new Binding("Text")
//newway
Binding b = new Binding("newMonth")
the path you are giving to the Binding should be the Path to the property you want. where you are setting the source you might even be able to leave that blank
+1 tam, and don't forget about the source:
Binding b = new Binding("newMonth"){
Source = this, // the class instance that owns the property 'newMonth'
ValidatesOnExceptions = true,
Mode = BindingMode.TwoWay,
};
In Flex I'm using the following code to allow sorting in a DataGrid (the data is paged and sorted serverside).
private function headerReleaseHandler(event:DataGridEvent):void
{
var column:DataGridColumn = DataGridColumn(event.currentTarget.columns[event.columnIndex]);
if(this.count>0)
{
if(this.query.SortField == column.dataField)
{
this.query.SortAscending = !this.query.SortAscending;
}
else
{
this.query.SortField = column.dataField;
this.query.SortAscending = true;
}
this.fill();
}
event.preventDefault();
}
This works perfectly, except that the arrows that indicate sorting isn't shown. How can I accomplish that?
Thanks!
/Niels
There is an example here if this is what you are looking for:
http://blog.flexexamples.com/2008/02/28/displaying-the-sort-arrow-in-a-flex-datagrid-control-without-having-to-click-a-column/
It looks like you need to refresh the collection used by your dataprovider.
I have encountered the same problem and the only solution I found was to override the DataGrid and create a custom one.
Here is the class:
public class DataGridCustomSort extends DataGrid
{
public function DataGridCustomSort()
{
super();
addEventListener(DataGridEvent.HEADER_RELEASE,
headerReleaseHandlerCustomSort,
false, EventPriority.DEFAULT_HANDLER);
}
public function headerReleaseHandlerCustomSort(event:DataGridEvent):void {
mx_internal::sortIndex = event.columnIndex;
if (mx_internal::sortDirection == null || mx_internal::sortDirection == "DESC")
mx_internal::sortDirection = "ASC";
else
mx_internal::sortDirection = "DESC";
placeSortArrow();
}
}
You have to specifically call the placeSortArrow() method when you get the HEADER_RELEASE event and set the column index and direction information.
in the above code what does "this" refer to is it the datagrid because I am confused by this.query.SortField , I am assuming 'this' and "query' are your own custom objects. and why are you checking for count. what count is that.
Regards
-Mohan