AvaloniaUI - How to choose folder - c#

I am using AvaloniaUI https://avaloniaui.net/docs/
I have researched their docs but it seems I can not find how can I make button which when pressed is forcing you to choose a folder.
Is it even possible and if so how, is there any example ?

I toyed with AvaloniaUI some time ago, got it working under Windows and struggled getting it working under Mac.
Nevertheless, I've seen your other question where you seem to get the dialog opened. Still, for the future:
In your XAML you place a button in a place you please:
<Button Content="Choose folder..." Margin="3" Name="FolderButton" />
Perhaps there is another way of getting it working, the following worked for me:
In your code you need to create a variable that represents your button:
private Button _folderButton;
In your constructor or in your InitializeComponent() method you find the button from XAML and assign it to your variable:
_folderButton = this.FindControl<Button>("FolderButton");
You also assign an event handler for Click event:
_folderButton.Click += FolderButtonClick;
You can immediately add the unsubscribe in your destructor:
_folderButton.Click -= FolderButtonClick;
Now you provide an event handler declaration and implementation:
public void FolderButtonClick(object sender, RoutedEventArgs e)
{
...
}
You may use http://avaloniaui.net/api/Avalonia.Controls/OpenFolderDialog/ - as you've already found out in your other question.
This even handler can be made async if you have any await operations inside.
I hope this helps.

Related

UWP: Binding + TextChanging = JIT win32 exception

I am 100% sure I am doing this the wrong way and this problem is "by design".
I want to have a Slider and a TextBox that displays its value. The user can either use the Slider, or manually enter a number in the TextBox.
I also wanted to take advantage of the TextChanging event to ignore any non-numerical entries. TextChanged would only function after a user has entered something and it's not a preferable scenario, and KeyDown would not capture other methods of input like ink or speech.
So I have this:
<StackPanel>
<Slider x:Name="Size" Value="100" Maximum="100" Minimum="0" />
<TextBox x:Name="SizeText" Text="{Binding ElementName=Size,Path=Value,Mode=TwoWay}" TextChanging="SizeText_TextChanging" />
</StackPanel>
Where "SizeText_TextChanging" is simply an empty block of code right now:
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
// Nothing Here.
}
This code builds, but at startup the app throws a JIT unhandled win32 exception and closes.
Changing TextChanging to TextChanged works fine, but again I prefer to get TextChanging to work (or something similar) to give a better user experience.
"Mode" also has no effect. I tried all three different Modes, all crash. By removing the binding altogether and giving the Text property any value works fine.
I also thought that maybe having the TextChanging event handler empty is the problem, so I borrowed the code below from here but the app still crashes:
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (!Regex.IsMatch(sender.Text, "^\\d*\\.?\\d*$") && sender.Text != "")
{
int pos = sender.SelectionStart - 1;
sender.Text = sender.Text.Remove(pos, 1);
sender.SelectionStart = pos;
}
}
Like I said, I am probably approaching this the wrong way. I am just starting to learn UWP and C# so I am a total noob. But I have read everything I could about TextChanging and it simply talks about rendering the value and the associated cautions of what not to write within the TextChanging event. So while it sounds like the app is being thrown into a loop trying to read the value of the slider and trying to see what the TextChanging event says, I don't see how to fix it. Please help!
Thank you
I don't know why this is happening, but a workaround is to register the TextChanging event handler only once SizeText (or the page) has loaded and using x:Bind instead of Binding:
XAML
<TextBox x:Name="SizeText" Text="{x:Bind Size.Value, Mode=TwoWay}" Loaded="SizeText_Loaded"/>
CS
private void SizeText_Loaded(object sender, RoutedEventArgs e)
{
SizeText.TextChanging += SizeText_TextChanging;
}
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
}
I meet the same problem but this solution doesn't works for me.
As #Mushriq, I use _TextChanging() to ignore any non-numerical entries in a form.
But my form contains a lot of numeric fields and also a master-detail part, which contains 2 of these fields.
The problem is that I enter in the _Loaded the first time that I display a part, but not for the other parts. When I display existing parts there is no problem, but if I add a new part I get the exception.
Is there a way to adapt the solution to my case?

c# - How to access a variable from outside its class in a method in some other class?

I am a beginner in c# programming and I am developing windows phone application after reading some tutorials.
My idea is when the user clicks a button in a windows page, some other button in other windows phone page must change color from red to green.
Pardon me if I am too Basic.
This I have defined in a page named "IndexPage.xaml"
<Button x:Name="One_green"
Content="1"
Background="Green"
Click="One_Click"
/>
<Button x:Name="One_red"
Content="1"
Background="Red"
Click="One_Click"
/>
Now I see red color button in my window as green button is hidden in the back.
Now, the following code is from another windows phone page "1.xaml"
<Button Content="GO" Click="Button_Click"/>
Now when the user clicks the "GO" Button I want the button to change to red to green in "IndexPage.xaml". So I tried a code something like this in "1.xaml.cs"
private void Button_Click(object sender, RoutedEventArgs e)
{
One_red.Visibility = Visibility.Collapsed;
One_green.Visibility = Visibility.Visible;
}
But I am not able to access the "One_red" or "One_green" button in the above code. Please shed me directions.
Also I want that code to execute only once. (i.e.) when the IndexPage.xaml loads again I want that button to be green always.
Thank you very much in advance.
Please tell me if some other details are required.
You could define a public or internal static variable inside the "Index.xaml" class specifying what button will show on load until otherwise specified. This variable could be accessed outside the class, and possibly even outside the project depending on the modifier chosen. The constructor of the "Index.xaml" class could have code to reset it to the default to ensure it only happens on the next creation of the page. If you aren't creating a new page everytime, you would have to put the default resetters in a method called when you want to bring it to foreground.
It seems to me that you are trying to learn, rather than having a SPEC to follow and implement.
Because of that, and because you are starting with C# in 2014 (almost 2015),
it will be quite beneficial for you to jump straight to data-binding declarative over imperative, going MVVM (MVVx) over MVC (MVx).
XAML was designed around this pattern. It's the natural way of doing things in XAML, a perfect fit and the perfect platform to learn the pattern.
It requires lots of learning, thinking, and re-learning, but it will open your eyes to modern programming techniques.
That said... there are too many ways of doing what you asked for, and while none are exactly wrong, there are 2 current trends in .Net/C#/MsTech which IMO are NOT a waste of your time:
Functional Reactive Programming and OOP/MVVx (the x is for whatever).
Examples are ReactiveUI, Reactive Extensions, PRISM, Caliburn.Micro and many more. They can be combined, the same way you can combine traditional event-driven/event callbacks with MVVM and/or Reactive Programming. However, I would advise against it.
I'll start with the most documented way.
Look at Data binding for Windows Phone 8. It was the first result when I googled "windows phone 8 xaml data binding," and deals with Colors and controls.
If you follow that example and add a resource to your application, you are done.
Of course, you can still use event => onClick + static class to hold the value in between View instances, but if I was right on the assumption that you are trying to learn, I wouldn't go that route.
Sorry if I drifted. :)
You may not be able to access the button click event because it is private, you may need to make it protected or public, the default access specifier would probably be ok as well.
public void Button_Click(object sender, RoutedEventArgs e)
or default would be:
void Button_Click(object sender, RoutedEventArgs e)

Win Phone App: Possible to use event declared on another page?

My scenario is quite simple, in fact I thought it would cause no problem but it does, can anyone help?
My MainPage begins with this:
namespace PhoneApp
{
public partial class MainPage : PhoneApplicationPage { /* snip */ }
}
Later in the page is this simple method:
private void MakeSound(object sender, MouseButtonEventArgs e)
All right, everything is fine.
But on my SettingsSample.xaml I have a ListBoxItem that calls the method MakeSoundvia SelectionChanged property. VS studio tells me that 'event handler MakeSoundis not found on class PhoneApp.SettingsSample '
The code for this page begins with:
namespace PhoneApp
{
public partial class SettingsSample : PhoneApplicationPage { /* snip */ }
}
Copying the code of the method on SettingsPage.xaml.cs doesn't work for some reason, but I feel like it should possible to use the method described on MaingePage.xaml.cs, especially since their respective code resides in the same Namespace.
I tried to add
using PhoneApp;
on SettingsSample.xaml.cs. Not exceptionally clever, but I have no other idea
Your event function should read EventClickOnMyListBox and be located in the code-behind for your page. This function would then call your makeasound function (that you can put wherever you want). And it could be called by another event or function.
There is a semantics distinction between the event itself and what the event does. Your event is not makeasound, your event will cause a sound to be made, among possible other things.
If ever you want to add a popup to add a visual effect, you'll be stuck if you called directly the makeasound function, whereas you'll just have to add a line to your EventClickOnMyListBox in the other scenario.
That's why xaml works that way, and allows you to refer only to object in the enclosing class (or if there is a way I don't know it / and never needed it). You should minimize code-behind code (this is the way of thinking of the MVVM pattern if you want to go further down this road).
Replying to your comment, here is some code to get you started:
Your event should be in the code-behind of your SettingSample page, and should call the makeasound function instead of being the makeasound function:
private void MenuItem_Click(object sender, MouseButtonEventArgs e)
{
Whatever.Myclass.MakeASound (blabla); // So typically MakeASound would be static
}
And now same thing in your main page: you do not use your private void MakeSound(object sender, MouseButtonEventArgs e) as the event for a click, but an event handler just as the one above.
Take a step back and you'll see it's very natural: you want two things to do the same. But one thing cannot be the exact same thing as the first, for language-related reasons (read about C# and XAML to understand why).
So you put your feature in a third component, accessible from the first two. And you call this component from each of them. This way, they both share access to the same feature, and everybody is happy.

How to add an event handler for events from button in C# from source view (aspx)

What is the easiest way to create code-behind (webforms) event handlers for say a button from HTML source view?
In VB.NET it is quite easy to switch to code behind page and use the object and events combo boxes along the top to select and create.
In c# those are missing (and I really don't like design view).
Make sure the Properties window is open.
Click anywhere in the element in source view.
Click the lightning symbol (events) in the Properties window.
Find the event you want to create a handler for.
Double click it.
I agree that this is less trivial with C# then with VB. My personal preference is to simply add a function with the following signature (always works):
protected void MyButtonName_Clicked(object sender, EventArgs e)
{
Button btn = (Button) sender; // remember, never null, and cast always works
... etc
}
Then, inside the code view of the HTML/ASP.NET part (aka the declarative code) you simply add:
<asp:Button runat="server" OnClick="MyButtonName_Clicked" />
I find this faster in practice then going through the several properties menus, which don't always work depending on focus and successful compile etc. You can adjust the EventArgs to whatever it is for that event, but all events work with the basic signature above. If you don't know the type, just place breakpoint on that line and hover over the e object when it breaks to find out the actual type (but most of the time you'll know it beforehand).
After a few times doing this, it becomes a second nature. If you don't like it, wait a moment for VS2010, it's been made much easier.
Note: both VB and C# never show the objects or events of elements that are placed inside naming containers (i.e., GridView, ListView). In those cases, you have to do it this way.

In Silverlight, what's the difference between UserControl_Loaded and Page_Loaded?

I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).

Categories