So I'm dabbling in C# properly for the first time trying to make a WPF based desktop application.
So far it's mostly going well, however as part of the project I am trying to take input from the user in one window (essentially where they define a project and the settings they want) and save them for later user in the project and have it act accordingly based on their input.
I've figured out the saving of this data for text inputs etc, however I'm having issues replicating this for check boxes.
I've defined the setting in the settings page as a bool defaulting to false, with the intent to be if the user ticks the checkbox then set the setting to true for later use.
When I'm trying to use .Checked against my checkbox class name it says it must appear on the left hand side of of += or -=, I've looked online for clarification and most similar code & relevant tutorials define it the way I have without issue.
Here's a snippet of the code:
public void CXML_GetSettings()
{
CXML_NewProject_Inc_SubModule_XML.Checked = Properties.Settings.Default.CXML_Project_Inc_SubModule;
}
Tried various ways of changing it but just can't get .Checked to work anywhere.
The Checked is an event for CheckBox, if you want to use it for your CheckBox, you can use it like below:
public MainWindow()
{
InitializeComponent();
MyCheckBox.Checked += MyCheckBox_Checked;
}
private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
Or
<CheckBox IsChecked="False" Name="MyCheckBox" Checked="MyCheckBox_Checked"/>
private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
Related
I am working on a project / settings page that has a toggle / switch within Xamarin Forms. I am using xamarin essentials for storing user data, etc.
The goal is to have the switch / toggle save the position of the toggle when a user makes a change. And have this bound / binding to the app settings. I already have push notifications installed however I want to give the user the option to unsubscribe or to subscribe again.
Based on OneSignals documentation here: https://documentation.onesignal.com/docs/xamarin-sdk#section--setsubscription-
This allows for the SetSubscription to be true or false. OneSignal.SetSubscription(false);
This is my SettingsPage.xaml
<Switch IsToggled="True" Toggled="OnToggled" />
This is my SettingsPage.xaml.cs
void OnToggled(object sender, ToggledEventArgs e)
{
// Perform an action after examining e.Value
}
My Goal is to have some app properties set and placed within the App.xaml.cs to be enabled on app launch and changed based on if the app contains the following keys to enable or disable notifications.
This is my App.xaml.cs
public App()
{
InitializeComponent();
if (Application.Current.Properties.ContainsKey("PushDisabled"))
{
//Do things when push is disabled...
OneSignal.SetSubscription(false);
Xamarin.Essentials.Preferences.Set("SetSubcription", false);
}
else
{
Application.Current.Properties["PushDisabled"] = false;
//Do things when push is enabled...
OneSignal.SetSubscription(true);
Xamarin.Essentials.Preferences.Set("SetSubcription", true);
}
OneSignal.Current.StartInit("one-signal-key").EndInit();
}
Note: OneSignal.SetSubscription(false); also causes this error: 'OneSignal' does not contain a definition for 'SetSubscription'.
Which is confusing, because it does reference it being here: https://documentation.onesignal.com/docs/xamarin-sdk#section--setsubscription-
I need help creating the proper code to actually set the key when a user has disabled notifications and to set it back to enabled when a user has clicked to toggle / switch to active again.
Any help or suggestions is appreciated the StackOverflow community has been very helpful for me reviewing other questions however could to find anything on this topic...
Since you are already using Xamarin Essentials for storing data. Try to store the toggled information in the Preferences and access it in your ViewModel to Bind to the UI. Refer to the below code.
public App()
{
InitializeComponent();
Xamarin.Essentials.Preferences.Set("SetSubcription", true);
OneSignal.SetSubscription(true);
}
Your view model must have a property accessing the value from the preferences.
public bool IsToggledValue
{
get { return Xamarin.Essentials.Preferences.Get("SetSubcription", false); }
}
Then you can bind the property from the ViewModel to your View in whichever page you are using it.
<Switch IsToggled="{Binding IsToggledValue}" Toggled="OnToggled" />
Please note that the BindingContext for your Switch must be ViewModel to get this logic working.
In your OnToggled callback, you can decide whether to subscribe to the push notifications or not.
private void OnToggled(object sender, ToggledEventArgs e)
{
if(e.Value)
{
OneSignal.SetSubscription(true);
}
else
{
OneSignal.SetSubscription(false);
}
}
Hope this helps you. Please let me know you need any further clarification.
OneSignal.Current.StartInit("app-id-code-here").EndInit();
if (!Application.Current.Properties.ContainsKey("PushDisabled"))
{
//Prepare notification for first time.
App.Current.Properties.Add("PushDisabled", false);
App.Current.SavePropertiesAsync();
Xamarin.Essentials.Preferences.Set("SetSubcription", true);
OneSignal.Current.SetSubscription(true);
}
I apologize if the question title isn't really specific, I'm not exactly sure how to condense the problem I'm having down to a few words. But to simplifiy the problem I'm having, here is my issue:
I'm creating a tool using WPF that consists of a TextBox that will contain a path to a directory and a Button that will allow you to Browse to a certain directory. Now, when I select the Browse button, it pops up a dialog, allows the user to select a directory and then I have some methods that will disable some buttons and updates some Brushes on the screen if the path doesn't meet a certain set of criteria. No problems there, got that working.
My problem is the TextBox that this Browse button correlates with. This TextBox is using a binding as such:
In my MainWindow.xaml (Yes, this is the simplified, focused version):
<Window>
<TextBox Text="{Binding Directory}" TextChanged="Directory_TextChanged" />
<Button Content="Browse..." Click="Browse_Click"/>
</Window>
In my code MainWindow.xaml.cs file:
public partial class MainWindow: Window
{
private ViewModel myViewModel;
public MainWindow()
{
myViewModel = new ViewModel();
this.DataContext = myViewModel;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Dialog stuff that's working
viewModel.Directory = dialog.SelectedPath;
}
private void InstallDir_TextChanged(object sender, TextChangedEventArgs e)
{
ValidatePath(); /* Disables/enables buttons and updates brushes based on validation. Also working */
}
private void ValidatePath() {/* */}
}
Like I mentioned earlier, the browse button works fine. I'm trying to figure out however, how I can get this to work if I type a directory alongside it. Because if I type something in the textbox, that would mean that inside of the InstallDir_TextChanged() function I would have to set viewModel.Directory, but since I have the INotifyPropertyChanged attached to this ViewModel, this function would get called recursively.
I tried doing the validation stuff within the viewmodel, but I couldn't figure out how to update the brushes/buttons in MainWindow if I did this. (Still relatively new to C# so I haven't learned the ins and outs yet. This is the first WPF tool I've been making from scratch, so just a disclaimer).
Would anyone have any ideas (or logic) I can approach to try and accomplish this? If there's any further clarification needed, that's not an issue. I don't need an exact definitive answer. Maybe some advice that could point me in the correct direction would definitely suffice. I don't have a problem trying to figure stuff out.
After hours of trying to solve this, I'm giving up and am actually wondering if there's a solution for this.
I have a WPF View that is setting the focus on a decimal up down from the code behind (xaml.cs). My aim is to select the value inside it since it loads "0.00" and it's frustrating for the user to have to delete it before he enters his value. I tried the following code in the loaded method:
private void Loaded_Window(object sender, EventArgs e)
{
txtAmount.AutoSelectBehavior = AutoSelectBehavior.OnFocus;
txtAmount.Focus();
Keyboard.Focus(txtAmount);
}
I have also tried to set SelectAllOnGotFocus as true and it still did not work. Funnily enough, this selects the test if I put a debug point in the method so I'm thinking that it has to do with the loading of the User Control. Does anyone have any pointers of how to make this work?
The xaml decimalupdown:
<xctk:DecimalUpDown x:Name="txtAmount" d:LayoutOverrides="Height" Minimum="0" Maximum="99999999.99" ShowButtonSpinner="False" UpdateValueOnEnterKey="True" Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged}" AllowSpin="False" FormatString="N2" >
Try the following:
private void [NAME OF YOUR EVENT GOES HERE] (object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
{
SelectAll();
}
}
The "SelectAll" is really the main thing here, you can ignore the if statement i believe.
In the XAML you will have to add the property "GotFocus" to your control like so:
<xctk:DecimalUpDown GotFocus="[NAME OF YOUR EVENT GOES HERE]" />
So that whenever it gains focus, the first thing it does is select everything ready for editing.
I have a number of PubCenter ads on each Windows Phone Page. By default they are all disabled.
I use a Random Generator and a switch to select one to turn on. This works fine with AdDuplex, but when I use Pubcenter I only get an empty space where the ad should be. Does anyone know what I have to do to get these ads to start working?
Various reasons may cause the PubCenter control to be hidden.
The simplest thing you can do to determine why is to subscribe to the AdControlError event in the code-behind of the page, like this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myAdControl.AdControlError += AdControlError;
}
private void AdControlError(object sender, ErrorEventArgs e)
{
string error = e.ErrorDescription;
MessageBox.Show(error);
}
I assume you have register with pubcenter, and are setting the AdControl.ApplicationId and AdControl.AdUnitId properties? if you have done that then it should all be good to go.
And these are the settings while you are designing/developing the app:
adControl1.ApplicationId = "test_client";
adControl1.AdUnitId = "Image480_80";
I'm attempting to use the ScintillaNET control in an application I am working on. I drag and drop the control into my form and run form. The control appears on the form. This is good. In addition, if I set any of the properties in the control's properties editor (ConfigurationManager.Language, for example), I am able to type in that language and see syntax highlighting occur.
Where I run into problems is when I attempt to change properties programmatically. For example, I attempt to load text from a file into the form (I'm doing this in the form's Load). The text doesn't display. I also can't seem to show the line numbers or do any other number of tasks (including programmatically change the Language).
Any idea what I may be doing wrong? Even something as simple as the code below doesn't seem to work:
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
}
Simply add scintilla1.ConfigurationManager.Configure();
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
scintilla1.ConfigurationManager.Configure();
}
After spending some time playing around with the different events, it appears that I cannot affect the Scintilla control until after it is already visible. Hence, the "Load" event does not let me make any programmatic changes to the control until I've set it visible.
It's a little strange, and seems sort of pointless to me to have the Load event at all, but I just wanted to let everybody know what is happening in case someone else ran into the same problem.