You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UWP C#报错:无法将string转换为byte[]/DateTime/bool 求助

Fixing String-to-Specific-Type Conversion for Your UWP Registration Page

Hey there, let's work through this type mismatch issue you're hitting. Since all your input fields are TextBoxes (which output strings), but your WCF service expects DateTime, byte[], and bool types, we need to safely convert each string input to the correct type before calling RegisterUserAsync. Here's how to handle each case:

1. Converting String to DateTime (DateOfBirthBox.Text)

First, you'll want to validate and parse the date string into a DateTime object. Using DateTime.TryParseExact is safer than plain Parse because it lets you enforce a specific date format (e.g., yyyy-MM-dd, MM/dd/yyyy) that matches what your users are expected to input. This avoids unexpected failures from inconsistent date formats.

DateTime dateOfBirth;
if (!DateTime.TryParseExact(DateOfBirthBox.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOfBirth))
{
    // Show error to user: "Please enter a valid date in YYYY-MM-DD format"
    var dialog = new MessageDialog("Please enter a valid date in YYYY-MM-DD format.");
    await dialog.ShowAsync();
    return;
}

2. Converting String to bool (EULicenseBox.Text, NonEULicenseBox.Text)

Assuming your users are entering values like "true"/"false", "yes"/"no", or "1"/"0" for these boolean fields, you can parse them to bool with validation. For example:

bool euLicenseAccepted;
if (!bool.TryParse(EULicenseBox.Text, out euLicenseAccepted))
{
    // Fallback logic if parsing fails, e.g., check for "yes"/"no"
    euLicenseAccepted = EULicenseBox.Text.Equals("yes", StringComparison.OrdinalIgnoreCase);
}

bool nonEuLicenseAccepted;
if (!bool.TryParse(NonEULicenseBox.Text, out nonEuLicenseAccepted))
{
    nonEuLicenseAccepted = NonEULicenseBox.Text.Equals("yes", StringComparison.OrdinalIgnoreCase);
}

Pro tip: If these are "accept terms" style fields, consider using a CheckBox instead of a TextBox—this avoids string parsing entirely and gives a better user experience!

3. Converting String to byte[]

Since you can't directly input a byte array into a TextBox, I'm assuming you're storing the byte data as a Base64-encoded string (a common approach for transmitting binary data as text). To convert this back to a byte[]:

byte[] euLicenseBytes = null;
try
{
    // Replace with the TextBox that holds your Base64-encoded byte array
    euLicenseBytes = Convert.FromBase64String(EULicenseBox.Text);
}
catch (FormatException)
{
    var dialog = new MessageDialog("Invalid license data. Please enter a valid Base64 string.");
    await dialog.ShowAsync();
    return;
}

Note: If the byte[] field is tied to a file (like a license file), you might want to replace the TextBox with a file picker control to get the byte array directly, instead of relying on manual Base64 input.

Putting It All Together

Here's your updated code with all conversions and validation:

// Validate and convert each field
DateTime dateOfBirth;
if (!DateTime.TryParseExact(DateOfBirthBox.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOfBirth))
{
    var dialog = new MessageDialog("Please enter a valid date in YYYY-MM-DD format.");
    await dialog.ShowAsync();
    return;
}

bool euLicenseAccepted;
if (!bool.TryParse(EULicenseBox.Text, out euLicenseAccepted))
{
    euLicenseAccepted = EULicenseBox.Text.Equals("yes", StringComparison.OrdinalIgnoreCase);
}

bool nonEuLicenseAccepted;
if (!bool.TryParse(NonEULicenseBox.Text, out nonEuLicenseAccepted))
{
    nonEuLicenseAccepted = NonEULicenseBox.Text.Equals("yes", StringComparison.OrdinalIgnoreCase);
}

byte[] euLicenseBytes = null;
try
{
    euLicenseBytes = Convert.FromBase64String(EULicenseBox.Text);
}
catch (FormatException)
{
    var dialog = new MessageDialog("Invalid license data. Please enter a valid Base64 string.");
    await dialog.ShowAsync();
    return;
}

// Now call the WCF service with the converted values
int i = await wcf.RegisterUserAsync(
    UserNameBox.Text,
    PasswordBox.Text,
    FirstNameBox.Text,
    LastNameBox.Text,
    dateOfBirth, // Pass DateTime instead of string
    euLicenseBytes, // Pass byte[] instead of string
    nonEuLicenseAccepted, // Pass bool instead of string
    EmailBox.Text,
    ContactNumberBox.Text
);

Double-check the parameter order in RegisterUserAsync to match your WCF service's actual expected types—I guessed based on your original code, so adjust if needed!

Additional Tips

  • Always add validation before conversion to catch bad input early and inform the user clearly.
  • For sensitive fields like passwords, use a PasswordBox control instead of a regular TextBox (it hides input and is more secure).
  • If your WCF service expects nullable types (e.g., DateTime?), you can use DateTime.TryParse and check if the parse succeeded before passing the value.

内容的提问来源于stack exchange,提问作者Armaan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:28:38