I am trying to build a menu structure with Monotouch.Dialog. The structure consists of multiple nested RootElements.
When you create a RootElement you set the caption in the constructor. This caption is used for the text of the table cell as well as the title in the view that follows when you click on it.
I would like to set the title of the view to a different text than the name of the element.
Let my try to illustrate what I mean with a simplified example.
The structure:
- Item 1
- Item 1.1
- Item 1.2
- Item 2
- Item 2.1
The code which creates this structure:
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow _window;
UINavigationController _nav;
DialogViewController _rootVC;
RootElement _rootElement;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
_window = new UIWindow (UIScreen.MainScreen.Bounds);
_rootElement = new RootElement("Main");
_rootVC = new DialogViewController(_rootElement);
_nav = new UINavigationController(_rootVC);
Section section = new Section();
_rootElement.Add (section);
RootElement item1 = new RootElement("Item 1");
RootElement item2 = new RootElement("Item 2");
section.Add(item1);
section.Add(item2);
item1.Add (
new Section()
{
new StringElement("Item 1.1"),
new StringElement("Item 1.2")
}
);
item2.Add (new Section() {new StringElement("Item 2.1")});
_window.RootViewController = _nav;
_window.MakeKeyAndVisible ();
return true;
}
}
When Item 1 is clicked, a screen is displayed which has the title "Item 1". I would like to change the title "Item 1" to "Type 1 items". But the text of the element which was clicked should remain to be "Item 1".
What is the best way of handling this?
It would be nice if I could do something like this:
RootElement item1 = new RootElement("Item 1", "Type 1 items");
I have tried getting the DialogViewController (see this post) and setting the Title of it. But I failed in getting this to work properly.
You can create a subclass of RootElement, that overrides the return value from GetCell and changes the text to render when it is part of a table view:
class MyRootElement : RootElement {
string ShortName;
public MyRootElement (string caption, string shortName)
: base (caption)
{
ShortName = shortName;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = base.GetCell (tv);
cell.TextLabel.Text = ShortName;
return cell;
}
}
Related
Working with the android studio Bottom Navigation activity template, want my HomeFragment / HomeViewModel page text to change each time the Home is accessed from the bottom navigation. When I click it the first time the text it displays should be different from the text displayed the 2nd time.
I'm able to create a variable that holds the text output, and I've created a function that should be able to update that variable but I'm unable to call it up within the MainActivity class. Any ideas?
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
navView.setupWithNavController(navController)
}
HomeFragment
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
var chosenOutput:String = "primary output";
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//using update here crashes the app
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = chosenOutput; //changing 'chosenOutput' to 'it' means that text update must take place within HomeViewModel instead of fragment.
})
return root
}
fun update(result: String)
{
chosenOutput = result;
}
}
HomeViewModel
class HomeViewModel : ViewModel() {
var temp:String = "demo"
//constructor()
private val _text = MutableLiveData<String>().apply {
value = temp;
}
fun update(result: String) //function may be obsolete if text change can be done in fragment.
{
temp = result;
}
val text: LiveData<String> = _text
}
What is the correct way to inject a string into either the Fragment or the ViewModel from the MainActivity class I've given?
You could change your approach and store your chosenOutput variable in the MainActivity
and update it everytime you select the HomeFragment
class MainActivity : AppCompatActivity()
{
var chosenOutput = "primary output"
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
navView.apply {
setupWithNavController(navController)
setOnNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.navigation_home -> {
chosenOutput = "new value" // <-- Update chosen output here
// Add this method to call the `onResume` method in the fragment
showFragment(HomeFragment.newInstance())
}
else { }
}
true
}
}
private fun showFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit()
}
}
Then you can retrieve the updated string in your Fragment with this code.
Note that I added the companion object to avoid the creation of multiple instances of the Fragment.
class HomeFragment : Fragment()
{
companion object
{
fun newInstance() : HomeFragment
{
return HomeFragment()
}
}
override fun onResume() {
super.onResume()
val newChosenOutput = (activity as MainActivity).chosenOutput
}
}
I spent so much time for discover how to implement TreeView in Android Xamarin, but unlucky seem not have any example say about that.
I tried to use ExpandableListView but it only support to level 2 category. I need someone will have any guide to through this content or some example say about that.
The purpose is explore the folders on server!
Thanks you so much.
The purpose is explore the folders on server!
You can use Binding Library to import some java library for example like AndroidTreeView.
For example, I created an .aar lib from this project. And then code for example like this:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//create root
TreeNode root = TreeNode.InvokeRoot();
TreeNode parent = new TreeNode("parent");
TreeNode child0 = new TreeNode("ChildNode0");
TreeNode child1 = new TreeNode("ChildNode1");
TreeItem item = new TreeItem() { text = "abc" };
TreeNode child10 = new TreeNode(item).SetViewHolder(new MyHolder(this));
child1.AddChild(child10);
parent.AddChildren(child0, child1);
root.AddChild(parent);
AndroidTreeView atv = new AndroidTreeView(this, root);
LinearLayout rootlayout = FindViewById<LinearLayout>(Resource.Id.rootlayout);
rootlayout.AddView(atv.View);
rootlayout.Invalidate();
}
The TreeItem is created like this:
public class TreeItem : Java.Lang.Object
{
public string text;
}
And MyHolder is like this:
public class MyHolder : TreeNode.BaseNodeViewHolder
{
private Context mcontext;
public MyHolder(Context context) : base(context)
{
mcontext = context;
}
public override View CreateNodeView(TreeNode p0, Java.Lang.Object p1)
{
var inflater = LayoutInflater.FromContext(mcontext);
var view = inflater.Inflate(Resource.Layout.itemview, null, false);
TextView tv = view.FindViewById<TextView>(Resource.Id.itemtv);
var item = p1 as TreeItem;
tv.Text = item.text;
return view;
}
}
Here is the demo, you can find the .aar lib there.
I have following generic class:
public class Member<T>
{
public bool IsDirty { get; set; }
public T Value { get; set; }
}
I want to create a custom editor for the PropertyGrid that will allow me to edit the IsDirty property through a CheckBox and the Value property through another nested editor.
With help I found here I've got this far:
class MemberEditor<T, TEditor> : ITypeEditor where TEditor : ITypeEditor
{
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
//var member = propertyItem.Value as Member<T>;
// checkbox for the Member.IsDirty value
var isDirtyCheckbox = new CheckBox();
var isDirtyBinding = new Binding("Value.IsDirty");
isDirtyBinding.Source = propertyItem;
isDirtyBinding.ValidatesOnDataErrors = true;
isDirtyBinding.ValidatesOnExceptions = true;
isDirtyBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(isDirtyCheckbox, CheckBox.IsCheckedProperty, isDirtyBinding);
// inner editor
var valueEditor = new TextBox();
var valueBinding = new Binding("Value.Value");
valueBinding.Source = propertyItem;
valueBinding.ValidatesOnExceptions = true;
valueBinding.ValidatesOnDataErrors = true;
valueBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(valueEditor, TextBox.TextProperty, valueBinding);
// compose the editor
var dockPanel = new DockPanel();
DockPanel.SetDock(isDirtyCheckbox, Dock.Left);
dockPanel.Children.Add(isDirtyCheckbox);
DockPanel.SetDock(valueEditor, Dock.Right);
dockPanel.Children.Add(valueEditor);
return dockPanel;
}
}
Now I am looking for a way to replace the TextBox, for something like this:
// ...
TEditor editorResolver;
PropertyItem innerPropertyItem;
// ... magic happens here ...
FrameworkElement valueEditor = editorResolver.ResolveEditor(innerPropertyItem);
// ...
The main goal is to avoid creating new class for each nested editor type.
Any ideas will be very much appreciated!
Take a look at the solution that I provided in this SO question, where I provide a custom editor via a button and a separate window.
I have been following the code from the monotouch.dialog task sample app (http://docs.xamarin.com/ios/Guides/User_Interface/MonoTouch.Dialog/Elements_API_Walkthrough).
Something I dont seem able to work out, is when the user clicks the + button a new row to the table is added. The user touches this and navigates to another screen where they can enter information. Now, when they navigate back to the root view, I want the list of rootElements to be updated so the entered name is used instead of the default name 'connection'
How would I go about updating the text for each of the RootElements based upon what has been entered?
I hope that all makes sense!
-- code below.
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
RootElement _rootElement = new RootElement ("To Do List"){ new Section()};
DialogViewController _rootVC = new DialogViewController (_rootElement);
// This adds an 'add' button to the root view controller, and handles by delegate,the push to a screen where you can add a new vCenter connection profile.
UIBarButtonItem _addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
_rootVC.NavigationItem.RightBarButtonItem = _addButton;
_addButton.Clicked += (sender, e) => {
var connectionProfile = new connectionProfile{};
// See, on the line below, I add a default RootElement with the text New Connection.
var connectionProfileElements = new RootElement ("New Connection") {
new Section () {
// When they enter the name on the next line of code, I want to use this to update the New Connection text on the root screen.
new EntryElement ("Name", "Enter Connection Name", connectionProfile._connectionName),
new EntryElement ("VC Address", "Enter VC Address", connectionProfile._address),
new EntryElement ("VC Port", "Enter VC Port", connectionProfile._port),
new EntryElement ("Username", "Enter Username", connectionProfile._userID),
new EntryElement ("Password", "Enter Password", connectionProfile._password,true)}
};
_rootElement [0].Add (connectionProfileElements);
};
UINavigationController _nav = new UINavigationController (_rootVC);
window.RootViewController = _nav;
window.MakeKeyAndVisible ();
return true;
}
}
And :
public class connectionProfile
{
public connectionProfile ()
{
}
public string _connectionName { get; set; }
public string _address { get; set; }
public string _port { get; set; }
public string _userID {get; set; }
public string _password { get; set; }
}
Did you try this.ReloadData(); like this ?
public NameOfYourDialogViewController() : base (UITableViewStyle.Grouped, null)
{
Root = new RootElement ("test") {
new Section ("First Section"){
new StringElement ("Hello", () => {
new UIAlertView ("Hola", "Thanks for tapping!", null, "Continue").Show ();
}),
new EntryElement ("Name", "Enter your name", String.Empty)
},
new Section ("Second Section"){
},
};
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Do your stuff
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.ReloadData();
}
You can find other topic about that here.
EDIT
You forgot to say that this is located in your AppDelegate, which is not made for what you are doing.
At first, create a DialogViewController: Project > Add new file > MonoTouch > DialogViewController.
And in the different method I mentioned earlier, put the ReloadData() method.
These methods (ViewDidLoad, WillAppear and so on) are override methods from UIView.
AppDelegate exists to get data before your is launched, store static data for your app, and so on. It's completly different usage.
And for your AppDelegate, you should have this for example:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
_window = new UIWindow (UIScreen.MainScreen.Bounds);
_controller = new NameOfYourDialogViewController();
_navigationController = new UINavigationController (_controller);
UIImageView splash = new UIImageView(_window.Bounds);
splash.Image = UIImage.FromFile("Default.png");
_window.AddSubview(splash);
_window.AddSubview(_navigationController.View);
_window.BringSubviewToFront(splash);
_window.MakeKeyAndVisible ();
// This is used to create a fadding effect in your splashscreen
UIView.Animate(1,
delegate { splash.Alpha = 0f; },
delegate {
_window.RootViewController = _navigationController;
splash.RemoveFromSuperview();
});
return true;
}
Using asp.net C#.
I have BulletList, and I would like to add ListItem that will render with childs.
That is, inside every <li> I can add more controls to be rendered.
How can I achieve that?
thanks
I'm not sure why BulletedList has a collection of ListItems. ListItems are generally used for form elements (thus the Selected, Value, Text properties) and doesn't make sense to be used for bulleted lists. There is also no property which contains child ListItems for you to accomplish your goal. I would suggest using your own classes to do this. Here is a quick mockup:
public static class BulletList
{
public static string RenderList(List<BulletListItem> list) {
var sb = new StringBuilder();
if (list != null && list.Count > 0)
{
sb.Append("<ul>");
foreach(var item in list) {
sb.Append(item.Content);
sb.Append(BulletList.RenderList(item.Children));
}
sb.Append("</ul>");
}
return sb.ToString();
}
}
public class BulletListItem
{
public string Content { get; set; }
public List<BulletListItem> Children { get; set; }
}
Then you can create your list with children and output it...
var items = new List<BulletListItem>();
items.Add(new BulletListItem() { Content = "Root 1" });
items.Add(new BulletListItem() { Content = "Root 2", Children = new List<BulletListItem>() { new BulletListItem() { Content = "Child 2.1" }} });
items.Add(new BulletListItem() { Content = "Root 3" });
Response.Write(BulletList.RenderList(items));