how to write code in cs file in xamarin.ios?

mc 4,006 Reputation points
2024-06-10T07:25:54.96+00:00

I create xamarin.ios. there is tabbed application

in the default file. there is Main.storyboard

some of it code is :

<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
    <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="First View" textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="KQZ-1w-vlD" misplaced="YES">
        <rect key="frame" x="221" y="279" width="157.5" height="41.5"/>
        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
        <fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="36"/>
        <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
        <nil key="highlightedColor"/>
    </label>
    <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Loaded by FirstViewController" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="A5M-7J-77L">
        <rect key="frame" x="203" y="329" width="194.5" height="17"/>
        <fontDescription key="fontDescription" type="system" pointSize="14"/>
        <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
        <nil key="highlightedColor"/>
    </label>


how to use the label?

in the controller customClass I can not get the label or other views. forexample label `

id="KQZ-1w-vlD"

`

how to change it's text in controller?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,319 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,321 Reputation points Microsoft Vendor
    2024-06-11T03:21:57.8066667+00:00

    Hello,

    You cannot get the label by that ID. The recommended way to build iOS user interfaces is directly on a Mac running Xcode.

    how to change it's text in controller?

    Opening your project via VS for Mac, double-clicking the Main.storyboard, which will show the visual representation by Xcode. After opening the storyboard, you must create an Outlet for the label that you wish to reference in C#.

    Detailed steps:

    1. Find the "Add editor on right" button on the toolbar, select the controller that you are creating the outlet for(ViewController.h).
    2. Click on the label, then Control Drag into the .h file.
    3. To Control Drag, hold down the control key then click and hold over the controller. Keep holding down the Control key while you drag into the header file (ViewController.h). Finish dragging below the @interface definition. Then you can set a property name.

    After that, you can set the text: this.MyLable = "111"

    For more details, you could refer to the Using Xcode Interface Builder section in the doc- Creating User Interface Objects in Xamarin.iOS - Xamarin | Microsoft Learn (the doc is about Xamarin, it applies to MAUI)

    Note: Xamarin support has been ended, please upgrade your Xamarin.iOS app to MAUI.

    See:

    Xamarin official support policy | .NET (microsoft.com)

    Xamarin.iOS project migration - .NET MAUI | Microsoft Learn


    Update

    You cannot edit the storyboard via VS 2022(Windows), it's not supported on a Windows machine. You could open your project by VS for Mac or VS code(Mac), then open the storyboard by Xcode, and then refer to the steps above to add a property (Label) for your Controller and set the text.

    Visual Studio for Mac is scheduled for retirement on August 31st, 2024 in accordance with Microsoft’s Modern Lifecycle Policy. It's still supported (nearly ending).

    Also, the main issue is how to edit your storyboard file, the underlying issue is to call Xcode.

    (The iOS Designer was deprecated in VS and VS for Mac, the recommended way to build iOS user interfaces is directly on a Mac running Xcode.)

    Another option on Mac is to use the preview version of the new C# Dev Kit extension for VS Code.

    Please see Visual Studio for Mac Retirement Announcement

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful