I'm trying to display several lines into a Text area from a string list. But it's only displaying the last one of the list, and i would like to display them all. Thanks for your help !
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class contenuTexte : MonoBehaviour {
public List <string> montexte;
public GameObject zoneAffichage;
// Update is called once per frame
void Start ()
{
foreach ( string lines in montexte)
zoneAffichage.GetComponent<Text>().text = "\n"+lines;
}
}
Your problem is that you are resetting the string on every loop to the last string. At the end of the loop you are going to only have the last string in the text.
First let's join all the strings together as one (using .ToArray() because of .NET 3.5), and then set them all at once:
var completeText = String.Join("\n", montexte.ToArray());
zoneAffichage.GetComponent<Text>().text = completeText;
Related
What is the best way to display font icons with data being pulled from a web service? I want to store the value of the icon and display the icon similar to the rest of the information, but with a custom font icon.
I have a xamarin forms app working with data from a webservice successfully. Here is my model.
public class TypeModel
{
[PrimaryKey]
public int pType { get; set; }
public int fDepartment { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
public string Version { get; set; }
}
I also got the font icons working successfully like the tutorial at [1]: https://montemagno.com/using-font-icons-in-xamarin-forms-goodbye-images-hello-fonts/ I got my icons stored under this class. Trying to transition to managing these values with web service or array if I have too now.
public const string IconCheckMark = "\uf12c";
public const string IconSearchGlass = "\uf349";
public const string IconQuestionMark = "\uf2d6";
public const string IconQuestionCircle = "\uf2d7";
But here is where my problem starts. My ViewModel is doing the work to create the collection. How do I assign a value that will display and match correctly.
public ObservableCollection<TypeModel> TypesCollection { get; set; }
TypesCollection.Clear();
IEnumerable<TypeModel> types = await DataSource.GetTypes();
foreach (var key in types)
{
\\Original value doesn't display correctly, shows like \U f 1 2 c when debugging on emulator
\\key.Icon = key.Icon;
\\Works and displays correctly but static data displays one icons for all
\\key.Icon = "\uf12c";
\\Works and displays correctly but static data displays one icons for all
\\key.Icon = FontClass.IconCheckMark;
TypesCollection.Add(key);
}
Nice and simple after that for the xaml files.
<ResourceDictionary>
<OnPlatform x:Key="IconFonts" x:TypeArguments="x:String">
<On Platform="iOS" Value="Material Design Icons"></On>
<On Platform="Android" Value="IconFonts.ttf#Material Design Icons"></On>
<On Platform="UWP" Value="/Assets/IconFonts.ttf#Material Design Icons"></On>
</OnPlatform>
</ResourceDictionary>
<Label FontFamily="{StaticResource IconFonts}" Text="{Binding Icon}"></Label>
What is the type of key.Icon? If it is string, you can remove the white space between the characters by:
key.Icon = key.Icon.Replace(" ", "").ToLower();
Got the answer from Microsoft Dev Community. First to explain what was happening: When you put the following in C# code:
Icon = "\uf2d6"
The “\uf2d6” is not actually a literal string, but rather it ends up being a single character (the one represented by that UTF code) at compile time. In other words the “\u” means that what follows is a hex code for a character and that whole “string” will end up just being that one character. If you inspect the Icon property after the above assignment, you will see that the property only contains a single character, not the “\uf2d6” string.
So when you get “2d6” from your web service in the Icon property, and then try to add “\uf” before it, you are not creating the escape sequence for the character but rather adding to the string literal. So what you need to do is parse the string for the hex number into an int and then cast it to a char and then assign that character to your Icon property. You can do this as follows (put this in the foreach loop in the sample you sent me):
string utfCode = $"f{key.Icon}"; // Adds the leading “f” to make it a correct hex number for the desired UTF code.
int character = int.Parse(utfCode, System.Globalization.NumberStyles.HexNumber); // parses that string literal into an int
key.Icon = ((char)character).ToString(); // casts that int to a char and then gets the string representation for that character and assigns it back to your Icon property
TypesCollection.Add(key);
Unity3D 2018.2
Problem: Grid not being populated by list, not responding correctly to
the List<> being filled with Transforms, contains at least 1 item in List<> so it should not be empty. Something is not being transferred right
I'm trying to create a Grid Layout in a scroll view, which is filled with buttons containing transforms kept in a List<>
I get the transforms from checking a GameObject which will usually have 0-25 child transforms in it.
Once it gets all the child transforms from the parent's GameObject, check which child has a tag called "Satellite". After fill the Grid with the List<> containing those certain gameObject.transforms.
Clicking the buttons in the grid should contain the transform, for example OnMouseEnter() in the script if I use Debug.Log(transform.name) it should display it.
Here is the code I'm using which contain no errors, the grid is empty so I'm not receiving the transforms correctly but I don't know what is wrong with the code. Thank you for the help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SatelliteGridControl : MonoBehaviour {
private List<Transform> satelliteListFromPlanet;
[SerializeField]
private GameObject buttonTemplate;
[SerializeField]
private GridLayoutGroup gridGroup;
[SerializeField]
private Sprite[] iconSprites;
// Use this for initialization
void OnEnable()
{
getSatellitesInPlanet();
satelliteListFromPlanet = new List<Transform>();
for (int i = 1; i <= satelliteListFromPlanet.Count; i++)
{
SatTransfrom newSatellite = new SatTransfrom();
newSatellite.iconSprite = iconSprites[Random.Range(0, iconSprites.Length)];
satelliteListFromPlanet.Add(newSatellite);
}
GenInventory();
}
// Get Satellites
private void getSatellitesInPlanet()
{
satelliteListFromPlanet = new List<Transform>();
// Get current planet
Transform currentPlanet = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<HandleCamera>().targetToLookAt;
// Check inside for satellites
foreach (Transform satellite in currentPlanet)
{
// Check transform for tag
if (satellite.CompareTag("Satellite"))
{
// Add each transform from planet to array
satelliteListFromPlanet.Add(satellite);
}
}
}
// Handle Grid
private void GenInventory()
{
if (satelliteListFromPlanet.Count < 6)
{
gridGroup.constraintCount = satelliteListFromPlanet.Count;
}
else
{
gridGroup.constraintCount = 5;
}
foreach (SatTransfrom sat in satelliteListFromPlanet)
{
GameObject newButton = Instantiate(buttonTemplate) as GameObject;
newButton.SetActive(true);
newButton.GetComponent<SatelliteButton>().SetIcon(sat.iconSprite);
newButton.transform.SetParent(buttonTemplate.transform.parent, false);
}
}
public class SatTransfrom : Transform
{
public Sprite iconSprite;
}
}
In OnEnable you first call getSatellitesInPlanet to populate your list satelliteListFromPlanet.
But right after you finished you call
satelliteListFromPlanet = new List<Transform>();
Which resets your list to a new empty one.
Than you have a loop
for (int i = 1; i <= satelliteListFromPlanet.Count; i++)
{ //... }
But since satelliteListFromPlanet is an empty list at this moment nothing happens.
And finally when you call GetInventory your list is still empty so
foreach (SatTransfrom sat in satelliteListFromPlanet)
Is executed never since there are no elements in satelliteListFromPlanet.
Now to the second problem:
You have
for(int i = 0; i< sateliteLostFromPlanet.Count; i++)
But inside of this loop you do
sateliteListFromPlanet.Add(xy);
... So what happens to your List during running this loop?
It grows bigger 1 elememt each loop so your loop condition i < sateliteListFromPlanet.Count will allways be true since after every execution your list is 1 element longer and i is 1 bigger!
Result: You add more and more elements to the same list "forever" until your device runs out of memory.
I have a pattern image that I need to repeat in my Gtk::DrawingArea using cairomm.
I tried using Cairo::ImageSource without luck.
It's easy using Cairo::Pattern class:
bool MyDrawingArea::on_draw(const Cairo::RefPtr<Cairo::Context> &cr) {
Cairo::RefPtr<Cairo::Pattern> image = Cairo::SurfacePattern::create(Cairo::ImageSurface::create_from_png("my_bg_pattern.png"));
image_->set_extend(Cairo::EXTEND_REPEAT); // THIS IS THE IMPORTANT BIT!
cr->set_source(image);
cr->paint();
}
I'm using the Create Item save action in a web form. I have a checkbox list that points to a folder containing category items. This is mapped to a Multilist field on the item template that it will create when the user submits the form. But it's passing the checkbox text, not the value, so the multilist for each new item created has bad data in it. Does anyone know how to set the checkbox list to pass the values instead? I'm kind of surprised it's doing this in the first place.
I haven't had a chance to test this, but theoretically, you could create a new field type that inherits from CheckboxList, and let's say we call it CheckboxListPipedValues. The code would look something like this:
using System.ComponentModel;
using System.Linq;
using System.Text;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Form.Web.UI.Controls;
public class CheckboxListPipedValues : Sitecore.Form.Web.UI.Controls.CheckboxList
{
[Browsable(false)]
public override ControlResult Result
{
get
{
StringBuilder stringBuilder1 = new StringBuilder();
var checkedItems = this.InnerListControl.Items.Where(a => a.Selected).ToList();
var values = string.Join("|", checkedItems.Select(c => c.Value));
foreach (var item in checkedItems)
{
stringBuilder1.AppendFormat("{0}, ", item.Text);
}
return new ControlResult(this.ControlName, values, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
}
}
}
In Sitecore, simply go to /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/List Types/Checkbox List and duplicate that item. Then change the assembly and class to the new control. Change your form to use the new field, and ensure that the values are mapped properly. Now the output of the value should be a pipe separated list of the values which should work nicely with the multilist field.
EDIT:
For MVC, it's the same process, but you'll need to update the MVC Type in the field type item to point to your new class. The code for MVC should look something like this:
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Forms.Core.Data;
using Sitecore.Forms.Mvc.Data.TypeConverters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.Mvc;
public class CheckBoxListPipedField : CheckBoxListField
{
public CheckBoxListPipedField(Item item) : base(item)
{
}
public override ControlResult GetResult()
{
var values = new List<string>();
StringBuilder stringBuilder1 = new StringBuilder();
if (this.Items != null)
{
foreach (SelectListItem selectListItem in
from item in this.Items
where item.Selected
select item)
{
values.Add(selectListItem.Value);
stringBuilder1.AppendFormat("{0}, ", selectListItem.Text);
}
}
var results = string.Join("|", values);
return new ControlResult(base.ID.ToString(), base.Title, results, stringBuilder1.ToString(0, (stringBuilder1.Length > 0 ? stringBuilder1.Length - 2 : 0)));
}
}
I have added an List box and and i have an list of value that needs to be populated in the list box. The only option i could find to add values to the list box is listbox.addItem... where i have to iterate the list of values i have and need to add it one by one. Is there any other way where i can add the entire list in one single call.??
private final List<Operation> numberComparisons = Arrays.asList(Operation.EQUAL, Operation.GREATER_THAN, Operation.GREATER_THAN_OR_EQUAL, Operation.LESS_THAN, Operation.LESS_THAN_OR_EQUAL, Operation.FILLED_IN, Operation.EMPTY);
now i have to add this number comparisons list into
ListBox conditionDropDown = new ListBox();
conditionDropDown.addItem(numberComparisons);
..... how can i do this...???
Maybe try this:
class MyListBox extends ListBox {
public void addAsList(List<Operation> list) {
for (Operation operation : Operation.values()) {
addItem(operation.toString());
}
}
and finally:
MyListBox conditionDropDown = new MyListBox();
conditionDropDown.addAsList(numberComparisons);
Have a look at the ValueListBox widget; you'll then use a Renderer to "generate" the String representation of your Operation (used to display them to the user in the list).